1. Don’t use automatically generated methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############ | |
## WRONG ## | |
############ | |
if course.visible | |
# do something | |
end | |
############## | |
## RIGHT ## | |
############## | |
if course.visible? | |
# do something | |
end |
2. N+1 problems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############ | |
## 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 |
- “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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
## 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”
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
## 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############# | |
## 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 |
If you are not able to roll back, so what’s point in migrations!?
https://jetruby.com/
Comments
Post a Comment