has_and_belongs_to_many关联不起作用

Teo*_*Teo 7 ruby many-to-many ruby-on-rails associations

has_and_belongs_to_many我的Ruby On Rails项目中存在关联问题.

这是我的模特:

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
Run Code Online (Sandbox Code Playgroud)

这是我的联接表迁移:

create_table "furnitures_stores", :id => false, :force => true do |t|
  t.integer "furniture_id"
  t.integer "store_id"
end
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用seed.rb插入一些值:

Furniture.delete_all
furnitures =  Furniture.create([{name: 'aaaa 1'}])

Store.delete_all
storee =  Store.create([{name: 'S 1'}])
Run Code Online (Sandbox Code Playgroud)

但它不起作用; 我有这个错误:

**rake aborted!
uninitialized constant Store::FurnituresId**
Run Code Online (Sandbox Code Playgroud)

Mic*_*rie 8

你需要has_and_belongs_to_many :furnitureshas_and_belongs_to_many :stores.您需要引用模型,而不是外键.

有关详细信息,请参阅ActiveRecord关联指南.