Rails has_many 通过单一关联
这可能很简单,但我还没有能够解决这个问题。
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
has_many :through
不适用于连接模型上的has_one
关系;有关详细信息,请参阅以下 Rails 票证:如果可能,最好的解决方案可能是修改您的关系,以便预订
belongs_to
评级而不是has_one
评级。has_many :through
doesn't work withhas_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 thanhas_one
Rating.