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 [email protected].
To post to this group, send email to [email protected].
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.

Reply via email to