I always want to show what I posted only to myself who is logged in

【Overview】

1. Conclusion </ b>

What is the 2.where method </ b>

3. How to use </ b>

4. What I learned from here </ b>

  1. Conclusion

Use the where method </ b>!

  1. What is the where method

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!


3. How to use

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!

Referenced URL: [Rails] I want to output values that exist in different tables using the login user id [ActiveRecord]
5. What I learned from here (used in case of error)

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