1

これをグーグルで検索すると、Rails コア チームが Rails 4 のソリューションに取り組んでいることがわかりますが、それはまだ先のことです。

UsersCirclesは互いにhas_and_belongs_to_many関係があります。

私のスキーマは次のようになります。

  create_table "circles", :force => true do |t|
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "circles_users", :force => true do |t|
    t.integer  "circle_id"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "users", :force => true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.string   "password"
  end

しかし、私のコードでは、circles_controllerこれをやろうとすると:

  def create
    @circle = Circle.new(params[:circle])
    @circle.users << User.find(session[:user].id)
...

ブラウザに次のエラーが表示されます。

SQLite3::ConstraintException: circles_users.created_at may not be NULL: INSERT INTO "circles_users" ("circle_id", "user_id") VALUES (11, 5)

ここで虚偽であるcreated_atことについてどうすればよいですか?updated_at

4

4 に答える 4

0

これを試してみてください

def create
  @circle = Circle.new(params[:circle])
  @circle.users = [User.find(session[:user].id)]

うまくいかないかもしれません。ローカルで試したことはありません。残念ながら ^_^

于 2012-10-27T04:21:18.447 に答える
0

これを試して

def new
 @circle = Circle.new
 1.times { @circle.users.build } if @circle.users.nil?
end

def create
  @circle = Circle.new(params[:circle])
  @circle.update_attributes(session[:user]) # this will automatically update location table by using relationship
  @circle.save   
end

HABTMを参照

于 2012-10-26T07:03:40.897 に答える
-1

これらの日時フィールドには、現在のタイムスタンプを挿入するか、データを入力しない場合は null 許容フィールドに変更する必要があります。

于 2012-10-27T04:11:23.300 に答える