I have a User model and a Book model.
Book is declared to belongs_to :user.
How do I get all books belonging to a specific user?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You also need to declare in User:
After that you can access it via the ruby console or if you want it in rails you need to create a route in the routes.rb:
After that you can ask the controller for all books of one user.
Just call books on a user in the controller.
And if you somewhere else in your code need all books for a certain user (e.g. @user):