我有3 模型,用户,商店和产品。关系是User has_many stores
和Product has_many stores
。在这种情况下,如果我做current_user.stores,我会得到特定用户拥有的商店数组。但现在我想返回商店与某些产品,我可以访问product_id,我可以做 current_user.stores.where(:product_id=>1)
,但事情是在我的情况下,我不能访问product_id,所以其他方式可以我用来完成,也许has_many,范围,但我不知道如何。有人可以帮我吗?访问has_many协会的ID
def my_account
#@product=Product.find(params[:id]) ,this cant find the id
if user_signed_in?
@my_stores=current_user.stores.where(:product_id=>@product.id)
end
end
视图
<% @my_stores.each do |store| %>
<%=store.name%>
<%end%>
当您尝试'什么是你的错误所有的商店current_user.stores.where(:PRODUCT_ID => 1)',即是什么阻止你访问商店#PRODUCT_ID ? – Woahdae 2012-02-01 02:00:42
'current_user.stores.where(:product_id => 1)''但是'Product.find(params [:id])''返回不能找到它没有一个id,url没有明显的id。 – katie 2012-02-01 02:12:01