Common Mistakes in RoR
1. ActiveRecord transaction for saving multiple object Whenever saving multiple objects in a single request,the ActiveRecord::Base::transaction method is here to support atomicity. So, if any record has not saved the entire transaction, will get rolled back and nothing will be saved into ...
1. ActiveRecord transaction for saving multiple object
Whenever saving multiple objects in a single request,the ActiveRecord::Base::transaction method is here to support atomicity. So, if any record has not saved the entire transaction, will get rolled back and nothing will be saved into the database. Example:
ActiveRecord::Base.transaction do @users.each do |user| user.save! end end
2. Use find_each
Do not use each method for iterating large set of records. It will fetch all the record in a single query from the database and will dump into the memory, causing performance degradation. So, better make use of find_each method, this method not only etches record in batches from the database but yields those once at a time too.
Example:
User.where(active: true).find_each do |user| # codes end
3. Using delete_all:
ActiveRecord::Base::delete_all deletes the records from the database without execute any callback. So, This method must be used sensibly. And ActiveRecord::Base::destroy_all executes before_destroy and after_destroy callbacks.
4. Set the primary key explicitly for database views
Sometimes we need to make database views for complex data joined from different tables, to reduce the overheads of fetching data in a single query and also to support the searching and sorting based on the columns of joined tables. Try to set primary key explicitly in the respective model, this will provide the flexibility to define association with other models.
5. Using IN sub-queries
Should Not use queries like IN or NOT IN These queries must be written using EXISTS or NOT EXISTS predicates to avoid serious performance issues.
6. Use pluck
We often need to find a single column from the database, So many of us make use of map method on ActiveRecord::Relation.
Like:
User.active.select(:name).map(&:name)
Instead use pluck method:
Example:
User.active.pluck(:name)
7. Try to reduce redundancy
method for checking odd number:
def odd?(x) x % 2 == 0 ? false : true end
Instead make use of Ruby logics and simply it somewhat more.
def odd?(x) x % 2 != 0 end
8. Avoid N+1 queries
When we load a bunch of objects from the database, and then for each one of those objects we make 1 or more database query. Active Record lets you eager load any number of associations with a single Model.
Example:
Article.includes(:category, :comments)
9. ActiveRecord::Base::exists
Sometime we just need to check for existence of any record in the database.
User.count > 0
Instead use exists method as:
User.exists?
10. No schema constraints specifics
Without schema constraints, data may be subject to bug due to failing some query. Try to specify the constrains before migrating.
create_table "user" do |t| t.integer "age", default: 0 t.string "name", :null => false end