Thursday, January 29, 2015

Way to use Time and Date in Active Record Association

In Active Record associations we can write conditions also, in that case whenever you are using the association by default the condition also applied.  So it is a good practice to use condition in associations.

see the below example


The above example will return always the published comments for the user.

Problem will come, when you are using Date or Time in the condition.  Because the condition will be loaded at the time of starting your server or console.  So the date and time will be the server start time, and it won't change until you restart the server.  So you won't get expected results.

To avoid that, we should not use date and time in the association condition, we can achieve that by adding scopes with that association.

see the below example

Note: Use lambda or proc in scopes, otherwise the same problem what we trying to resolve will appear again.

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