0

我有两个型号

class LunchSet < ActiveRecord::Base
  has_many :sides
end

class Side < ActiveRecord::Base
  attr_accessible :set_id

  belongs_to :lunch_set, foreign_key: :set_id

end

但是,当我使用 id = 1 创建对象 new LunchSet 'l' 和 set_id = 1 的 new Side 's' 时,键入 l.sides 只会给我 sql 错误,上面写着

SQLite3::SQLException: no such column: sides.lunch_set_id: SELECT "sides".* FROM "sides"  WHERE "sides"."lunch_set_id" = 52

如您所见,它仍在寻找列“lunch_set_id”而不是我想要的“set_id”...

4

1 回答 1

1

你需要在LunchSet模型中定义外键

class LunchSet < ActiveRecord::Base
  has_many :sides, :class_name => 'Side', :foreign_key => 'set_id'
end

class Side < ActiveRecord::Base
  attr_accessible :set_id

  belongs_to :lunch_set, :class_name => 'LunchSet', :foreign_key => 'set_id'

end
于 2013-07-06T20:00:09.777 回答