How to write Rails

#Bad writing
@incorrect_questions = session[:incorrect_question_ids].map { |n| Question.find(n.to_i) }

#Why not?
#If this is the case, a query that calls the Question will be issued for each number of cases, and if there are 100, 100 queries will be issued, so this is better.

#Good writing
@incorrect_questions = Question.where(id: session[:incorrect_question_ids].map(&:to_i))

Rails way of thinking

-DHH's opinion is that it is better to use a different controller for actions that do not exist by default, and I think so too. ・ What is REST? ・ One of the object-oriented ideas is that an object has its own state. ・ It is dangerous to use session, so why not prepare the person's condition at the table? ・ Avoid the name Test ◯◯ as it may be mistaken for an rspec file. -Since the method runs unintentionally in the callback system (before_save etc.), it is not very preferable to write the logic there.

#Bad writing
def index
  @questions = Question.all
end

#Why no?
#I think that there is no problem with the current number, but if you use all in the actual service, you should be careful because memory may be insufficient and it may drop.
Basically, dynamically changing data (for example, user-added data) is select/I think it is better to make a habit of limiting the required columns with pluck etc.
#Bad writing
<%= form_for(@question_similar, url: { controller: 'question_similar', action: 'create' }) do |f| %>

#Reasons not good
url: {〜〜〜}Is xxx_It seems that you can write with path
#Bad writing
 def cancel_save
    question_similars.each do |question_similar|
      if question_similar.similar_word == ""
        question_similar.delete
      end
    end
  end

#.Use blank
#Transaction okay?
#delete_I think all has better performance
params[:front_article_search_form][:heart_period]

This kind of hash is called a two-dimensional hash, If params [: front_article_search_form] is nil in the first place Errors like undefined method `[]'for nil: NilClass (NoMethodError) often occur. So you have to be careful about how to write it.

If it was me

article_search_form = params[:front_article_search_form]
if article_search_form.present?
 @heart_period = article_search_form[:heart_period]
end

Recommended Posts

How to write Rails
How to write Rails validation
How to write Rails seed
How to write Rails routing
[Rails] How to write in Japanese
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write dockerfile
How to uninstall Rails
How to write docker-compose
How to write Mockito
How to write migrationfile
Rails: How to write a rake task nicely
[rails] How to post images
How to write good code
[Rails] How to write when making a subquery
[Rails] How to use enum
Bit Tetris (how to write)
[Rails] How to install devise
[Rails] How to use enum
How to write java comments
How to read rails routes
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to use rails join
How to write Junit 5 organized
How to terminate rails server
[Rails] How to use validation
[Ruby] How to write blocks
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
How to write a date comparison search in Rails
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] Two ways to write form_with
[Rails] How to use flash messages
[rails] How to display db information
Studying Java # 6 (How to write blocks)
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Baseball ball count (how to write)
[Rails] How to add new pages
How to write a ternary operator
[Rails] How to install ImageMagick (RMajick)
How to write Java variable declaration
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
[Rails] How to implement star rating