has_and_belongs_to_many关联无法正常工作。

8
我在我的 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个回答

8
你需要使用 has_and_belongs_to_many :furnitureshas_and_belongs_to_many :stores。你需要引用模型而不是外键。
有关更多信息,请参见ActiveRecord关联指南

但是我该如何引用模型? - Teo
@Teo 我不知道你的意思。 - Michael Lawrie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接