如何通过关系中的has_many中的铁轨控制台检索对象?
因此,我有用户,航空公司和评论。 HAS_MANY:用户,通过::评论。用户has_many:评论has_many:airlines,tha ::评论和评论属于航空公司和用户。一个用户可以体验多个航空公司。我不理解一种获取所有评论的方法由特定航空公司旅行的用户。
here is my seed data
singapore_airlines=Airline.create(name: 'Singapore Airlines')
cathy=User.create(name:"Cathy",email:"[email protected]",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)
所以我在控制台中知道我可以做类似的事情 sing = airline.find(1),然后唱歌。回报..........但这将为我提供新加坡航空公司的所有评论。我只想要凯茜的评论。
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:"[email protected]",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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要直接执行此操作,您可以做:
或:
或:
...或者如果这是一个常见的查找,则可以考虑为此提供便利的方法,例如:
To do this directly, you could do:
Or:
Or:
...Or if this is a common lookup, you could consider making a convenience method for it such as: