调用轨道中的关联模型



我的关联是

restaurant.rb(型号(

has_many :products

和产品中。rb(型号(

belongs_to :restaurant

现在,在我的产品控制器中,我基本上正在尝试生成一个所有产品的列表,并通过一些验证来搜索它们。我只想展示那些属于特定餐厅的产品

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

最新更新