I'm trying to sort some items that I am receiving from my database. Currently, I have a series of objects that get returned from my Tool model. Right now, I am retrieving some tools from my database, by using the following code (in my toolkit_controller):
def index
@tools = Tool.order('name ASC').all
end
This seems to be working fine and as intended. When I do this, I get the following result set (omitted some of the extra results for clarity).
[#<Tool id: 1, name: "Hammer", description: "A basic hammer",
created_at: "2013-07-07 16:46:13",
updated_at: "2013-07-07 16:46:15">,
#<Tool id: 2, name: "Mallet", description: "A fancy
mallet", created_at: "2013-07-07 16:46:13", updated_at: "2013-07-07 16:46:15">,
#<Tool id: 3, name: "Screwdriver", description: "A screwdriver",
created_at: "2013-07-07 16:46:13", updated_at:
"2013-07-07 16:46:15">,
#<Tool id: 4, name: "Torch", description: "A cheap torch", created_at:
"2013-07-07 16:46:13", updated_at: "2013-07-07 16:46:15">, ....]
What I'm struggling with right now is how to display my tools in a vertical fashion (rather than horizontal) on my erb page. To help paint a clear picture of the sort order I want, please take a look at this post: Ruby on Rails change array display order.
My display code is as follows:
<%= @count = 0 %>
<div>
<% @tools.each do |t| %>
<% @count = @count+1 %>
<div class="individualTool">
<%= t.name %> <%= @count %>
</div>
<% if @count == 4 %>
<% @count = 0 %>
</div>
<div class="row-fluid">
<% end %>
<% end %>
As you can see, the above code works fine to display the tools by name from left-to-right (e.g. Hammer, Mallet, Screwdriver, Torch), but my goal here is to display them from top-to-bottom, in an order as seen here: Ruby on Rails change array display order.
In the post that I linked, someone suggested using .each_slice(3).to_a.transpose.flatten (to try and correct the sort order), but when I tried that on the @tools that were being returned, I received an error message stating that the element size differs.
Any ideas on how I can accomplish this would be great!
Thanks in advance.
toolsprogramatically if that's possible. If I could correct the order in which they exist [#Tools ... ], then I think my problem would resolve itself and the code would work.