Saturday, January 3, 2015

Avoid dynamic finder method for single attribute using lazy-attribute gem

Lazy Attribute

This gem provides a simple and extremely flexible way to use most frequent find_by query in a model.  You can minimize find_by query of a particular attribute in a model using lazy attribute gem.

Overview

Generally in your application, if you want to fetch a user by id, you can directly use the find method
User.find(12345)
In the same way, if you want to find any user with their username / email, they have to use any one in the below
User.find_by_email('user_email@sample.com')
User.where(email: 'user_email@sample.com').first
User.find_by(email: 'user_email@sample.com')
The lazy_attribute gem will provide way to simplify the above code.


Implementation

Add this line to your application's Gemfile:
gem 'lazy-attribute'
Add this code to your model file
class User < ActiveRecord::Base
  lazy_attribute :email
end
Then anywhere in your application you can find the user with the email attribute like below
User['user_email@sample.com']
This will return user object and will return nil in case of the user not present in the db.  And you can pass optional argument like below
lazy_attribute :email, :raise_error => true
 This will raise ActiveRecord::RecordNotFound Exception instead of sending nil on record absence case.


Source


No comments:

Post a Comment