Reputation: 14938
Easy question: I need help writing an ActiveRecord find_all_by
statement where I want to order things from newest created objects to oldest.
As a follow-up to this question: If I saved the results into a variable @records. How do I then ask for the next record in it? I want to do something like @current_record = @records.next
Upvotes: 4
Views: 1105
Reputation: 28392
This code will do your initial query
@records = Model.find_all_by_column('value', :order => 'created_at DESC')
And with regards to your follow up. If you want to iterate over the returned array then you can do the following
@records.each do |record|
# Code to do something
end
Within the iterator you could set a flag when you see a particular record then do something with the next record in the iterator.
If you wish to check whether there are any records in the array returned by the finder then you can check @records.length
Upvotes: 6
Reputation: 4598
for question 2: what do you mean by "the next record in it" ?
if you search for a way to display results, it's convenient to use each:
<% @records.each do |record| %>
#...
<% end %>
Or even better, with a partial
<%= render :partial => 'record', :collection => @records %>
Upvotes: 0
Reputation: 54593
Model.find_all_by_column_name('something', :order => "...")
Upvotes: 0