Using Deligate in ActiveRecord to Avoid Dots

Adding deligate method in the model

There are various situations where we might be using methods from the child model from its parents model.

Common example is user and posts

1
2
3
4
5
6
7
class Post
  belongs_to :user
end

class User
  has_many :posts
end

To make a call to find a post with category ‘ruby’ for a User

1
User.first.posts.find_by_category('ruby')

If this is being used in multiple places, adding a deligate will make our life easy, thereby avoiding mulitple dots. Also User model doesnt have to know the inner methods of the posts model and a logic change in the model Post would not require multiple changes

1
2
3
4
class Post
  belongs_to :user
  delegate :find_by_category, :to => :user, :allow_nil => true
end

now the following code will work.

1
User.first.find_by_category('ruby')

There is an option to allow prefix as well

Any questions on this, please feel free to ask. We’re here to help…

Comments

Comments