has_and_belongs_to_many关联无效

时间:2013-06-12 17:43:49

标签: ruby-on-rails ruby many-to-many associations

我在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**

1 个答案:

答案 0 :(得分:8)

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

有关详细信息,请参阅A Guide to ActiveRecord Associations