[英]Calling associated models in rails
My association is我的协会是
restaurant.rb (model) restaurant.rb(模型)
has_many :products
and in products.rb (model)并在 products.rb (模型)
belongs_to :restaurant
Now in my products controller i am basically trying to generate a list of all products and search them with some validation.现在在我的产品 controller 中,我基本上是在尝试生成所有产品的列表并通过一些验证来搜索它们。 I want to show only those products which belong to a specific 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
In the 'else' block of case i want products according to restaurant id.在“其他”案例中,我想要根据餐厅 ID 的产品。 How can i do that?
我怎样才能做到这一点?
Product.where(restaurant_id: some_id).all
You could also do something like你也可以做类似的事情
Restaurant.find(restaurant_id).products
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.