1

我有以下模型和运行 rails 3.01:

# file: app/models/product.rb
class Product < ActiveRecord::Base
  has_many :categories, :through => :product_categories
  has_many :product_categories, :dependent => :destroy
  accept_nested_attributes_for :product_categories
end

# file: app/models/category.rb
class Category < ActiveRecord::Base
  has_many :products, :through => :product_categories
  has_many :product_categories, :dependent => :destroy
end

# file: app/models/product_category.rb
class ProductCategory < ActiveRecord::Base
  belongs_to :product
  belongs_to :category
end

ProductCategory 是我的连接表。我在我的产品表格中叫什么?我是在 Categories 表还是 ProdcutCategories 表上构建的?我真的很困惑我应该如何/哪些模型嵌套在我的产品中。谢谢!

4

1 回答 1

1

该模型已配置为接受products_categories关联的属性。在您的表单中,只需像这样引用它:

<%= f.fields_for :products_categories do |pc| %>
  # fields go here

products_categories请记住,在此表单呈现任何内容之前,您需要为关联构建新对象:

products.products_categories.build
于 2012-07-17T03:38:20.837 回答