我有一个名为ProductCategory(有许多产品)和一个模型产品(属于ProductCategory)的模型。我播种product_categories表这个数据在这里(我相信你已经猜到了,它只有两个的cols(类别和category_type)。获取类别及其所有产品
product_categories = [
{:category => "Arts", :category_type => "physical" },
{:category => "Books", :category_type => "physical" },
{:category => "Comics", :category_type => "digital" },
{:category => "Diy & Craft", :category_type => "physical" },
{:category => "E-books", :category_type => "digital" }
]
现在在我的产品指标我要显示所有类别随着10个随机产品在每个类别。后来我会改变到前十。
我最终希望实现的一个例子是This http://s866.photobucket.com/user/tommyadey/media/products.jpg.html
但在我的情况下,我想显示所有的类别和看到每个类别的十种产品 我试过了:
ProductCategory.includes(:products).limit(10)
什么是最好的方式去做这件事? 我不确定这是否意味着先进或简单,对不起,如果它相对容易,我还在学习。 谢谢。
你的意思是属于产品类别权10级随机的产品呢? – usha
嗯,这是正确的:) – Skyalchemist