re: What's the best way to render specific items from a Rails collection? VIEW POST


This doesn't answer your question, but have you thought about storing the position as an integer in the database? Would that be an option? Depending on what you want to do, it might be over-engineering, but it could simplify the view. If you had a position column, you might do something like this:

In the controller:

# Margarita (position == 1), Bourbon (position == 2), Beer (position == 3), and so on
@cocktails = Cocktail.order(:position)

In the view:

<%= render @cocktails %>

<!-- or -->

<% @cocktails.each do |cocktail| %>
  <%= cocktail.name %>
<% end %>

It might be something to take a look into as you think about drink groups.


That makes sense! It’s a good solution. I’ll look into it. Thanks!

code of conduct - report abuse