views:

203

answers:

3

Hello,

I want to display "tips" on my page that changes every say 30 seconds. The results from the "find" method in rails is not ordered by ID. It will be ordered by another column.

What is the best way fetch "next" value. Is there a way to get number of row in result set and then find by next row id?

Thanks,

Tam

+1  A: 

You could just make an ajax call and get a random tip. Something like

@tip = Tip.find(:first, :order => "RAND()")
render :update do |page|
  page.replace_html :tip_container, :partial => "tip"
end
Maran
+1  A: 

Use find with :offset => N, :limit => 1 and simply count up on N.

sbwoodside
Thanks...Although the other solutions work, I liked this one best because in this case I don't have to fetch all of them at once as there might be many to put in session. Also, I wanted them in specific order as opposed to random.
Tam
A: 

If you've already fetched all the tips into an array you can step through it using that arrays index, @results[1].tip, @results[2].tip, etc.

Jarrod