Wednesday, December 28, 2011

Simplified Active Record Extensions

by Sandip Ransing 0 comments

Below extensions to active record base simplifies its usage while coding.
# config/initializers/core_extensions.rb class ActiveRecord::Base def self.pagination(options) paginate :per_page => options[:per_page] || per_page, :page => options[:page] end def self.options_for_select(opts={}) opts[:name] ||= :name opts[:attr] ||= :id opts[:prompt] ||= 'Please Select' all.collect{|c| [c.send(opts[:name].to_sym), c.send(opts[:attr].to_sym)]}.insert(0, [opts[:prompt], nil]) end end Usage
class Student< ActiveRecord::Base #attributes #=> name, age end ## Student.pagination({:page => 10}) Student.pagination({:per_page => 2, :page => 2}) Student.options_for_select Student.options_for_select({:prompt => 'Please select'}) Student.options_for_select({:name => :age})

blog comments powered by Disqus

About The Author

Sandip is a ruby on rails developer based in pune and also a blogger at funonrails. Opensource contributor and working with Josh software Private Limited. for more info read Follow Sandip on Twitter for updates.

Connect With Me...

Github Projects

@sandipransing Twitter