如何通过关系中的has_many中的铁轨控制台检索对象?

发布于 2025-02-13 00:11:13 字数 899 浏览 0 评论 0原文

因此,我有用户,航空公司和评论。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一绘本一梦想 2025-02-20 00:11:13

要直接执行此操作,您可以做:

singapore_airlines.reviews.where(user_id: cathy.id)

或:

cathy.reviews.where(airline_id: singapore_airlines.id)

或:

Review.where(user_id: cathy.id, airline_id: singapore_airlines.id)

...或者如果这是一个常见的查找,则可以考虑为此提供便利的方法,例如:

class Airline < ApplicationRecord

  # ...

  def reviews_for_user(user)
    reviews.where(user_id: user.id)
  end

  # ...

end

# Usage:

singapore_airlines.reviews_for_user(cathy)

To do this directly, you could do:

singapore_airlines.reviews.where(user_id: cathy.id)

Or:

cathy.reviews.where(airline_id: singapore_airlines.id)

Or:

Review.where(user_id: cathy.id, airline_id: singapore_airlines.id)

...Or if this is a common lookup, you could consider making a convenience method for it such as:

class Airline < ApplicationRecord

  # ...

  def reviews_for_user(user)
    reviews.where(user_id: user.id)
  end

  # ...

end

# Usage:

singapore_airlines.reviews_for_user(cathy)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文