1. Conclusion </ b>
What is the 2.where method </ b>
3. How to use </ b>
4. What I learned from here </ b>
Use the where method </ b>!
A method to find the conditions you want!
model.where(Condition name:Contents of the condition)
Can be used in!
Frequently used to describe programs that search for specific characters in the search word field of blogs and news articles!
I used it as below!
time_controller.rb
@times = Time.where(user_id: current_user.id).includes(:user).order("created_at DESC")
I used the where method because I wanted to display what was posted by the currently logged-in user only to the person who posted it = the logged-in user (current_user.id)! includes (: user) solves the N + 1 problem (reduces unnecessary processing of the program), and order ("created_at DESC") is in the order of new posts!
I tried to separate cases with if conditional expressions, but if I do not limit the display itself no matter how many conditional expressions I make, all will be displayed under the conditional expression. The where method was so conscious of creating a search word field that the essence was "restricting the display". Concrete event ➡︎ I was convinced that converting to an abstract event would broaden the scope of utilization for methods.
Recommended Posts