【发布时间】:2018-04-06 16:43:08
【问题描述】:
我是 RoR 的新手。我正在建立电子商务网站,其中我有两个模型产品和类别如下:
Products : has_many :categories_products, dependent: :destroy
has_many :categories, through: :categories_products
Categories: has_many :categories_products
has_many :products, through: :categories_products
Categories_products: belongs_to: products, belongs_to: categories
我的网站有 megamenu 类别。我的意图是单击其中任何一个类别并获取属于它的所有产品。到目前为止,我只能在使用参数获取类别时获取 category_id。
if params[:category]
@categoryproducts = :category.products.all.paginate(page: params[:page], per_page: 20)
else
redirect_to root_url
end
关于使用上述方法
:category:Symbol 的未定义方法 `products'
我在网上搜索了大多数都说使用连接表。提前感谢您的帮助。
【问题讨论】:
-
将
:category更改为Categories或:category.products更改为Categories_products -
能否请您重申,因为 category_products 只会给我产品 id 和 category_id
-
另外,如果我改变 :category 那么我将如何获得没有参数的产品
标签: ruby-on-rails has-many-through