开发者

Rails has_many through singular association

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 see开发者_开发百科m 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.


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

  • https://rails.lighthouseapp.com/projects/8994/tickets/1149
  • https://rails.lighthouseapp.com/projects/8994/tickets/2719-has_many-through-should-work-if-the-join-model-uses-has_one-relationship

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜