我有以下关系:
用户.rb
has_many :ownerships
has_many :restaurants, :through => :ownerships
餐厅.rb
has_many :ownerships
has_many :users, :through => :ownerships
我也有一个菜单模型belongs_to :restaurant。
menu很明显,a和 a之间没有直接联系,user除非你通过restaurant.
我正在尝试使它只显示由创建的菜单user。
通过这种方法,我只能显示当前登录用户创建的餐厅:
restaurant_controller.rb
class RestaurantsController < ApplicationController
# GET /restaurants
# GET /restaurants.json
def index
if current_user.has_role? :admin
@restaurants = Restaurant.all
elsif current_user.has_role? :user
@restaurants = current_user.restaurants
end
end
我怎样才能通过通过在我的menus控制器下做同样的事情restaurant?
我试过了:
@menus = current_user.restaurants.menus
和
@menus = current_user.restaurants.each.menus
除其他外,但似乎没有任何效果。我总是正确地undefined method获得 nil:NilClass , which I understand; I'm not getting to themenus` 的菜单。
我正在考虑制作一个循环,通过每个循环restaurant并添加menu附加到它,但我不是 100% 确定如何在menu控制器中做到这一点。