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 %>
5 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.
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
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
<< Home