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 %>

19 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.

 
At Fri Jul 08, 05:19:00 PM 2016, Blogger ninest123 said...

air max, sac longchamp, ray ban sunglasses, longchamp pas cher, ugg boots, ray ban sunglasses, nike free, oakley sunglasses, nike free, chanel handbags, cheap oakley sunglasses, nike outlet, louboutin, longchamp, nike roshe run, air jordan pas cher, tiffany jewelry, longchamp outlet, longchamp outlet, michael kors, prada outlet, louis vuitton outlet, louboutin outlet, louis vuitton, uggs on sale, ugg boots, louis vuitton, polo ralph lauren outlet, oakley sunglasses, louis vuitton, kate spade outlet, louboutin pas cher, polo ralph lauren outlet, tory burch outlet, tiffany and co, oakley sunglasses, ray ban sunglasses, ralph lauren pas cher, christian louboutin outlet, nike air max, nike air max, louboutin shoes, replica watches, jordan shoes, oakley sunglasses, gucci outlet, prada handbags, burberry, replica watches, louis vuitton outlet

 
At Fri Jul 08, 05:23:00 PM 2016, Blogger ninest123 said...

ray ban uk, vanessa bruno, sac guess, air force, tn pas cher, coach outlet, michael kors, burberry outlet online, nike blazer, lululemon, vans pas cher, mulberry, michael kors, burberry, nike free run uk, nike roshe, ray ban pas cher, michael kors outlet, hollister pas cher, hollister, north face, hermes, new balance pas cher, coach purses, true religion outlet, nike air max, replica handbags, ugg boots, michael kors outlet, hogan, nike air max, true religion jeans, abercrombie and fitch, kate spade handbags, lacoste pas cher, true religion jeans, michael kors outlet, converse pas cher, coach outlet, timberland, nike air max, michael kors, ugg boots, michael kors outlet, true religion jeans, north face, michael kors, ralph lauren uk, oakley pas cher, michael kors outlet

 
At Fri Jul 08, 05:34:00 PM 2016, Blogger ninest123 said...

ray ban uk, vanessa bruno, sac guess, air force, tn pas cher, coach outlet, michael kors, burberry outlet online, nike blazer, lululemon, vans pas cher, mulberry, michael kors, burberry, nike free run uk, nike roshe, ray ban pas cher, michael kors outlet, hollister pas cher, hollister, north face, hermes, new balance pas cher, coach purses, true religion outlet, nike air max, replica handbags, ugg boots, michael kors outlet, hogan, nike air max, true religion jeans, abercrombie and fitch, kate spade handbags, lacoste pas cher, true religion jeans, michael kors outlet, converse pas cher, coach outlet, timberland, nike air max, michael kors, ugg boots, michael kors outlet, true religion jeans, north face, michael kors, ralph lauren uk, oakley pas cher, michael kors outlet

 
At Fri Jul 08, 05:37:00 PM 2016, Blogger ninest123 said...

wedding dresses, giuseppe zanotti, instyler, mont blanc, converse outlet, longchamp, babyliss, north face outlet, north face outlet, insanity workout, gucci, vans, hollister, asics running shoes, chi flat iron, ralph lauren, baseball bats, bottega veneta, timberland boots, mac cosmetics, valentino shoes, abercrombie and fitch, mcm handbags, hollister, lululemon, nike air max, hollister, celine handbags, herve leger, ghd, louboutin, soccer jerseys, p90x workout, beats by dre, vans shoes, ray ban, nike air max, nike trainers, new balance, ferragamo shoes, nike huarache, reebok shoes, lancel, nfl jerseys, nike roshe, oakley, iphone cases, jimmy choo shoes, birkin bag, soccer shoes

 
At Fri Jul 08, 05:54:00 PM 2016, Blogger ninest123 said...

bottes ugg, ugg pas cher, louis vuitton, pandora jewelry, moncler, moncler, doudoune canada goose, replica watches, louis vuitton, moncler, juicy couture outlet, moncler, juicy couture outlet, ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, pandora charms, canada goose, links of london, canada goose outlet, pandora charms, canada goose uk, canada goose, marc jacobs, moncler, wedding dresses, louis vuitton, canada goose outlet, karen millen, louis vuitton, moncler outlet, sac louis vuitton pas cher, coach outlet, moncler, swarovski, ugg boots uk, toms shoes, moncler, pandora jewelry, canada goose, montre pas cher, thomas sabo, supra shoes, hollister, swarovski crystal

 

Post a Comment

Links to this post:

Create a Link

<< Home