2013-06-12 75 views
7

我在我的Ruby On Rails項目中遇到關聯has_and_belongs_to_many的問題。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** 

回答

8

你需要has_and_belongs_to_many :furnitureshas_and_belongs_to_many :stores。您需要參考模型,而不是外鍵。

請參閱A Guide to ActiveRecord Associations瞭解更多信息。

+0

但我該如何參考模型? – Teo

+0

@Teo我不知道你的意思。 –

+0

我現在明白了..它的工作原理..謝謝 – Teo

相關問題