Rails model - next and previous objects
Its been a long time since, I posted anything. So, I thought I will let something go.
In one of my Rails application, while showing an object I need to show links to the previous and next objects. I think, this is a common task in many Rails application, the way I am doing it is I have extended the Active Record Base class so that all my models have this function. There are numerous way to include this, choose one which you think most suits your purpose or which you like.
module ActiveRecord #:nodoc:
class Base
def next_id
@next ||= self.class.find(:first,:select => ['id'],:conditions => ['id > ?', id], :order => 'id')
@next ? @next.id : nil
end
def previous_id
@previous ||= self.class.find(:first,:select => ['id'],:conditions => ['id < ?', id], :order => "id desc")
@previous ? @previous.id : nil
end
end
end
Let this file be called 'base_ext.rb' and put it in 'lib' folder of your application.
Now, in your model files add the line
require 'base_ext'
at the top this will ensure that this file is loaded before your model class is declared.
Now, in your views you can use something like this:
<%= link_to '« Previous',{:id => @creation.previous_id},:class => "alignleft" if @creation.previous_id %>
<%= link_to 'Next »',{:id => @creation.next_id},:class => "alignright" if @creation.next_id %>













14 Comments:
Nifty solution!
I changed it to find the next id in a table but by sorting on date.
def next_id
@next ||= self.class.find(:first,:select => ['id'],:conditions => ['datum > ?', datum], :order => 'datum')
@next ? @next.id : nil
end
def previous_id
@previous ||= self.class.find(:first,:select => ['id'],:conditions => ['datum < ?', datum], :order => "datum desc")
@previous ? @previous.id : nil
end
I'm dutch so datum means date .. :-)
Got it working and put the code in a model. I hope to get around to building a more generic solution, and use it like:
@object.previous_field(fieldname)
And then putting it back in base_ext.rb
I'll update you when it's build (if ever ... :-/ )
Anyway ... a good (and almost the only proper) solution to the next/previous item issue.
Didn't find any clean ones on ruby forums.
Great work, thanx again!!!
Gerard
Forgot my site .. :-)
Thanks Gerard.
Getting the previous field should be also simple.
def next_field(fieldname = 'id')
@next ||= .....
@next ? @next[fieldname] : nil
end
and then you can call it using
@object.next_field(:name) or whatever. Instead of @next[fieldname] you can also use @next.send(fieldname)
the above code is untested.
酒店打工
酒店兼職
台北酒店
打工兼差
酒店工作
酒店經紀
禮服店
酒店兼差
酒店上班
酒店PT
酒店
台北酒店 酒店兼差 酒店兼職 酒店
酒店兼差 酒店兼職 酒店 台北酒店
酒店兼職 酒店 台北酒店 酒店兼差
酒店 台北酒店 酒店兼差 酒店兼職
酒店經紀 酒店打工 酒店工作 酒店上班
酒店經紀 酒店打工 酒店工作 酒店上班
酒店經紀 酒店打工 酒店工作 酒店上班
酒店經紀 酒店打工 酒店工作 酒店上班
酒店經紀 酒店打工 酒店工作 酒店上班
There are many brand from France, also including herve leger, and most of womens stars love wearing herve leger dress when they join in some important party. Now polo ralph lauren is very popular with youthful people, everyone want to get ralph lauren polo shirts, there are lots of online shop which are ralph lauren polo outlet, true religion jeans outlet, it will be convenient for us.
Post a Comment
Links to this post:
Create a Link
<< Home