我的 Ruby On Rails 项目中的 has_and_belongs_to_many
关联有问题。
这是我的模型:
class Store < ActiveRecord::Base
attr_accessible :address, :city, :map_url, :name, :uimage_url
has_and_belongs_to_many :furnitures_id
end
class Furniture < ActiveRecord::Base
attr_accessible :description, :image_url, :maintenance, :name, :size
has_and_belongs_to_many :store_id
end
这是我的连接表迁移:
create_table "furnitures_stores", :id => false, :force => true do |t|
t.integer "furniture_id"
t.integer "store_id"
end
然后我尝试用 seed.rb 插入一些值:
Furniture.delete_all
furnitures = Furniture.create([{name: 'aaaa 1'}])
Store.delete_all
storee = Store.create([{name: 'S 1'}])
但它不起作用;我有这个错误:
**rake aborted!
uninitialized constant Store::FurnituresId**
最佳答案
您需要 has_and_belongs_to_many :furnitures
和 has_and_belongs_to_many :stores
。您需要引用模型,而不是外键。
参见 A Guide to ActiveRecord Associations获取更多信息。
关于ruby-on-rails - has_and_belongs_to_many 关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17071902/