[英]Calling associated models in rails
我的協會是
restaurant.rb(模型)
has_many :products
並在 products.rb (模型)
belongs_to :restaurant
現在在我的產品 controller 中,我基本上是在嘗試生成所有產品的列表並通過一些驗證來搜索它們。 我只想顯示屬於特定餐廳的產品
def index
case params[:status]
when 'Active'
active_products
when 'Inactive'
inactive_products
when 'Sold out'
sold_out
else
@products = Restaurant.Product.all #problem arises here
end
if params[:search]
search
end
if params[:section]
section
end
if params[:sort_by]
product_sort
end
end
在“其他”案例中,我想要根據餐廳 ID 的產品。 我怎樣才能做到這一點?
Product.where(restaurant_id: some_id).all
你也可以做類似的事情
Restaurant.find(restaurant_id).products
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.