Smorgasbord - Politics, Lisp, Rails, Fencing, etc.

My musings on assortment of things ranging from politics, computer technology and programming to sports.

Friday, August 04, 2006


On this day:

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:

At Fri Feb 02, 01:59:00 AM 2007, Anonymous Gerard said...

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

 
At Fri Feb 02, 02:00:00 AM 2007, Anonymous Gerard said...

Forgot my site .. :-)

 
At Wed Feb 07, 11:32:00 AM 2007, Blogger Surendra Singhi said...

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.

 
At Wed Aug 26, 07:01:00 PM 2009, Anonymous Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店上班

酒店PT

酒店

 
At Wed Jul 07, 10:27:00 PM 2010, Anonymous Anonymous said...

台北酒店 酒店兼差 酒店兼職 酒店

 
At Wed Jul 07, 10:27:00 PM 2010, Anonymous Anonymous said...

酒店兼差 酒店兼職 酒店 台北酒店

 
At Wed Jul 07, 10:28:00 PM 2010, Anonymous Anonymous said...

酒店兼職 酒店 台北酒店 酒店兼差

 
At Wed Jul 07, 10:28:00 PM 2010, Anonymous Anonymous said...

酒店 台北酒店 酒店兼差 酒店兼職

 
At Wed Jul 07, 10:28:00 PM 2010, Anonymous Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

 
At Wed Jul 07, 10:29:00 PM 2010, Anonymous Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

 
At Wed Jul 07, 10:29:00 PM 2010, Anonymous Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

 
At Wed Jul 07, 10:29:00 PM 2010, Anonymous Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

 
At Wed Jul 07, 10:29:00 PM 2010, Anonymous Anonymous said...

酒店經紀 酒店打工 酒店工作 酒店上班

 
At Fri Jan 28, 05:28:00 AM 2011, Blogger Cheapsocceruniforms said...

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