Wednesday, December 2, 2009

Extending Rails ActiveRecord::Base Class

Example:
1. Create a file(suppose init.rb) into your project's config/initializers folder

2. Paste the following code :
ActiveRecord::Base::class_eval do
def self.your_method
return 'Now I am in ActiveRecord'
end
end

3. Now you can call your_method from any Model of your application, i,e you are adding your_mothod(it will static method) to all your application's models

4. You can call it like :
User.your_method()
Post.your_method() etc...

No comments:

Post a Comment