Rails has_many 通过单一关联

发布于 2024-08-13 01:11:09 字数 629 浏览 7 评论 0原文

这可能很简单,但我还没有能够解决这个问题。

我有 3 个表...(远不止这个),但在这种情况下 3 个表很重要。

地点 预订和 评级

Places has_many bookings

每个预订都有一个评级(因为用户只评级一次)并且属于(a)地点

评级属于(a)预订

所以即使预订只有一个评级,通过预订也可以通过预订进行 has_many 评级,因为地方也有很多预订,

但这似乎不起作用?其余的似乎都这样。

Bookings 具有 place_id,Ratings 具有 booking_id,并且我通过关联成功地与所有其他 has_many 合作...只是 has_one 没有?有什么想法吗?谢谢

补充:如果我将 Bookings 更改为 has_many : ratings ,它会起作用...但 Bookings 每次预订只会有一个评级...所以这似乎不是一件好事。

Invalid source reflection macro :has_one for has_many :ratings, :through => :bookings.  Use :source to specify the source reflection.

This is probably quite simple but I haven't yet been able to wrap my head around the problem.

I have 3 tables... (well more than that) but in this scenario 3 that matter.

Places
Bookings and
Ratings

Places has_many bookings

Each booking has_one rating (because the user only rates once) and belongs_to (a) Place

Ratings belong_to (a) Booking

So even though bookings has only one rating, places has_many ratings through bookings as places has many bookings also

but this doesn't seem to work? All the rest seem to.

Bookings has place_id and Ratings has a booking_id, and I've been successful with all other has_many through associations... just not the has_one? Any ideas? Thanks

addition: if i change Bookings to has_many :ratings it works... but bookings will only ever have one rating per booking... so this doesn't seem like a good thing.

Invalid source reflection macro :has_one for has_many :ratings, :through => :bookings.  Use :source to specify the source reflection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

咆哮 2024-08-20 01:11:09

has_many :through 不适用于连接模型上的 has_one 关系;有关详细信息,请参阅以下 Rails 票证:

如果可能,最好的解决方案可能是修改您的关系,以便预订belongs_to 评级而不是has_one 评级。

has_many :through doesn't work with has_one relations on the join model; see the following Rails tickets for details:

If possible, the best solution is probably to modify your relations so that a Booking belongs_to a Rating rather than has_one Rating.

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