Find New Objects in ActiveRecord

A simple question: I need help writing the ActiveRecord find_all_by , where I want to order things from the most find_all_by created objects to the oldest.

To continue this question: If I saved the results in the @records variable. How can I ask for the next entry? I want to do something like @current_record = @records.next

+4
source share
3 answers

This code will fulfill your initial request.

  @records = Model.find_all_by_column('value', :order => 'created_at DESC') 

And regarding your observation. If you want to iterate over the returned array, you can do the following

  @records.each do |record| # Code to do something end 

Inside the iterator, you can set a flag when you see a specific record, then do something with the next record in the iterator.

If you want to check if there are any records in the array returned by finder, then you can check @ records.length

+6
source
 Model.find_all_by_column_name('something', :order => "...") 
0
source

for question 2: what do you mean by β€œnext entry in it”?

If you are looking for a way to display the results, it’s convenient to use each of them:

 <% @records.each do |record| %> #... <% end %> 

Or even better, with partial

 <%= render :partial => 'record', :collection => @records %> 
0
source

All Articles