2016-06-09 42 views
0

我正在构建Yelp类型的应用程序。我有一张餐桌,一张桌子,还有一个我已经命名的菜肴(dish_id & restaurant_id)。如果在排列中存在一排,那就意味着餐馆携带该物品。如何从另一个表中采摘并显示项目

我想添加一个菜单到餐厅展示页面,其中列出了与该餐厅相关的所有菜单项。

我该怎么做?

回答

0

在你restaurant模型中,添加此关联关系:

has_and_belongs_to_many :dishes, join_table: :dishings 

在你dish模型中,添加此关联关系:

has_and_belongs_to_many :restaurants, join_table: :dishings 

然后,把所有菜餐厅: restaurant.dishes

希望这会有所帮助。

+0

谢谢!运行'<%= restaurant.dishes%>提供以下输出:#'。你知道什么样的命令/循环我需要成功地吐出菜吗? –

+0

现在你有一系列的菜肴,这很好。那么你可以做 <%restaurant.dishes.each do | dish | %> (用碟子做点什么) <% end %> –

+0

靠近!我仍然收到NameError。这是我的有缺陷的代码:<%@ restaurant.dishes.each do | dish | %> <%= dishes.dish.pluck(:dish_name)%> <% end %> –

0

Thanks @ÉricCôté。这很好!

<% @restaurant.dishes.each do |dish| %> 
<%=dish.dish_name%> 
<% end %> 
相关问题