我有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