gpt4 book ai didi

ruby-on-rails - 在 Rails 3 中对关联模型进行排序?

转载 作者:行者123 更新时间:2023-12-03 18:18:25 26 4
gpt4 key购买 nike

菜单 has_many :dishes。

我想按 Dish.number 对菜肴进行排序。

目前在我看来它看起来像:

<table class="menu">
<% @menu.dishes.each do |dish| %>
<div class="dish">
<tr>
<td>
<div class="dish_div dish_name">
<% if @menu.name != 'Övrigt' && @menu.name != 'Box to go' %>
<span class="dish_name"><%= "#{dish.number}. #{dish.name}" %></span>
<% else %>
<span class="dish_name"><%= "#{dish.name}" %></span>
<% end %>

<% if dish.strength_id == 2 %>
<%= image_tag('chili.png') %>
<% elsif dish.strength_id == 3 %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<% elsif dish.strength_id == 4 %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<%= image_tag('chili.png') %>
<% end %>
</div>
<div class="dish_div"><%= "#{dish.description}" %></div>
<div class="dish_div dish_price"><%= "#{dish.price} kr" %></div>
</td>
</tr>
</div>
<% end %>
</table>

我怎么做?

它应该在 View 中还是 Controller 中?

谢谢

最佳答案

两者都不! :) --- 在你的模型定义中做

如果您总是想订购力量:

class Menu
has_many :dishes, :order=>'strength_id DESC'
end

class Dish
belongs_to :menu
end

否则,只需在 View 中订购:
<% @menu.dishes.sort_by{|dish| dish.strength_id}.each do |dish| %>

关于ruby-on-rails - 在 Rails 3 中对关联模型进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3419630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com