我有两个型号
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
但是,当我创建对象新的LunchSet'l',id为= 1,而新的side''s'with set_id = 1时,输入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'......
答案 0 :(得分: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