DEV Community

Discussion on: Polymorphic joins in Active Record

Collapse
 
vishaldeepak profile image
VISHAL DEEPAK

I think there's a mistake here ,

belongs_to :accommodation, -> { where(bookings: { bookable_type: 'Accommodation' }) }, foreign_key: 'bookable_id'

accomdation_id probably does not exist

Collapse
 
anakbns profile image
Ana Nunes da Silva

You're right Vishal, same for the office relation. Fixed both. Thanks for the heads-up!

Collapse
 
nrpx profile image
Arseniy Pelipas

@anakbns sorry, but you'll end up with an empty result subset. These JOINs are mutually exclusive. Possible use is left_joins.

Thread Thread
 
anakbns profile image
Ana Nunes da Silva • Edited

Thanks @nrpx! Yes, in this case inner joining both associations does not make sense since it will try to fetch bookings that belong both to an accommodation and an office, which is an impossibility here. Updated to use a left join instead. 👍