Brian Sammon
2014-Oct-21 21:04 UTC
building join queries and using the association name in the where method
I'm just learning how to build join queries in ActiveRecord, and I've noticed the following: If I have class Person < ActiveRecord::Base has_many :meetings, class_name: 'Appointments' end I can't do Person.joins(:meetings).where({meetings: {status: 'cancelled'}}) I must do Person.joins(:meetings).where({appointments: {status: 'cancelled'}}) (assuming the table underlying 'Appointment' is named 'appointments') This strikes me as somewhat un-DRY-ish. Some questions I have: Am I correct that I can't use the association name in the condition? Has this been previously discussed? In particular has there been discussion about adding this (the ability to use the association name) to Rails? Anybody got any interesting workarounds to suggest? Any links to stuff (webpages/mailing-list-archive-entries/github issues) I should read? Some questions I'm afraid to ask: What if my persons class has two relations that both refer to the Appointment class? What about Single Table Inheritance? I've noticed that there is a "meta_where" gem (https://github.com/activerecord-hackery/meta_where) that provided this for Rails 3.0, but it hasn't been updated for Rails 4, and its replacement/successor "squeel" doesn't seem to provide it. P.S. This is a contrived example, which doesn't represent my app as a whole. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/20141021170457.f5b944412952a0b284ee6d2b%40brisammon.fastmail.fm. For more options, visit https://groups.google.com/d/optout.