Common Model mistakes on Rails


1.  Don’t use automatically generated methods
############
## WRONG ##
############
if course.visible
# do something
end
##############
## RIGHT ##
##############
if course.visible?
# do something
end

2. N+1 problems
#############
## WRONG ##
#############
@homeworks = lesson.homeworks
- @homeworks.each do |homework|
%p homework.user.email
#############
## RIGHT ##
#############
@homeworks = lesson.homeworks.includes(:user)
- @homeworks.each do |homework|
%p homework.user.email
3. Don’t use scopes
############
## WRONG ##
############
def index
@lessons = Сourse.lessons.order(position: :asc)
end
############
## RIGHT ##
############
class Lesson < ActiveRecord::Base
belongs_to :course
scope :by_position, -> { order(position: :asc) }
end
def index
@lessons = course.lessons.by_position
end
4. Don’t know the difference between “after_create” and “after_commit”

  • “after_create” might result in invalid data if the ID is used before the transaction is completed.
  • Using “Sidekiq” or any other background worker I could always use “after_commit” to ensure the integrity of my data.
5. Always use CRM
#############
## WRONG ##
#############
Article.all.each { |article| article.delete }
Article.all.map { |article| article.title }
Course.all.select { |course| course.created_at < 5.years.ago }.each { |course| course.articles.delete_all }
#############
## RIGHT ##
#############
Article.delete_all
Article.pluck(:title)
old_courses_ids = Course.where(‘created_at < ?’, 5.years.ago’).pluck(:id)
Article.where(course_id: old_courses_ids).delete_all

6.  Don’t know the difference between “dependent destroy” and “delete_all”
#############
## WRONG ##
#############
class Article
validates :body, length: { minimum: 200 }
end
articles_data.each do |article_data|
Article.create(article_data)
end
#############
## RIGHT ##
#############
# There are 2 possible solutions
articles_data.each do |article_data|
Article.create!(article_data)
end
# In this case a developer will be able to see that data he was not expencting to receive will get on the input
articles_data.each do |article_data|
article = Article.new(article_data)
unless article.save
puts ‘Can not save article’
#process this situation
end
end
# Give a user a choice.
7.  Don’t set default fields in migrations
#############
## WRONG ##
#############
class Article
after_initialize :set_default_status
def set_default_status
self.status = ‘pending’
end
end
#############
## RIGHT ##
#############
class MyMigration
def up
change_column :articles, status, :string, default: ‘pending’
end
def down
change_column :articles, status, :string
end
end
8.  Don’t set constraints in migrations
#############
## WRONG ##
#############
class MyMigration
def change
add_column :profiles, user_id, :integer
end
end
#############
## RIGHT ##
#############
class MyMigration
def change
add_column :profiles, user_id, :integer, null: false
end
end
9.  Don’t write reverse migrations in migrations
If you are not able to roll back, so what’s point in migrations!?
https://jetruby.com/

Comments