lundi 4 juillet 2022

How to get retrieve objects in rails console in a has_many through relationship?

so i have users ,airlines and review .An airline has_many :reviews has_many :users,through: :reviews.A user has_many :reviews has_many :airlines,through: :reviews and a review belongs to an airlines and user.A user can experience multiple airlines.I dont understand a way to get all the reviews of a user of travelling by a particular airline.

here is my seed data
singapore_airlines=Airline.create(name: 'Singapore Airlines')

cathy=User.create(name:"Cathy",email:"cathy@jetsetgo.com",admin:false)


Review.create(image:"https://worldofwanderlust.com/wp-content/uploads/2022/03/866D61D2-FFD1-4392-9596-0E4E372D18D9.jpg", date: DateTime.new(2021, 11, 1),destination:"Singapore to Tokyo",seat:"Business class",description:"If you want comforting long flights always take Singapore airlines",likes:0,dislikes:0,airline_id:singapore_airlines.id,user_id: cathy.id)

so im aware in the console i can do something like sing=Airline.find(1) and then sing.reviews.........but that will give me all the reviews of singapore airline. i want only the review given by cathy.

Aucun commentaire:

Enregistrer un commentaire