Reputation: 1645
I've got a Person model, who has_many roles, and roles, in turn, belong_to an application. I'd like to query all the roles a person has for a given application. So far I've got:
p = Person.includes(:roles => [:application]).where(:loginid => 'their_loginid', :roles => {:application_id => 1})
Which works, but it's querying based on Person.roles.application_id; instead, I'd like to query based on Person.roles.application.api_key (another property of an application).
I tried:
p = Person.includes(:roles => [:application]).where(:loginid => 'their_loginid', :roles => {:application => {:api_key => 'the_api_key'}})
but I receive the error that:
no such column: application.api_key
leading me to think my usage of ActiveRecord isn't joining the tables together correctly.
Any ideas?
Upvotes: 1
Views: 1095
Reputation: 2356
try this
p = Person.joins.includes(:roles => [:application]).where(:loginid => 'their_loginid', :roles => {:application => {:api_key => 'the_api_key'}})
In my case it woks.
Upvotes: 0
Reputation: 9226
Try this:
p = Person.includes(:roles => [:application]).where(:loginid => 'their_loginid', :role_id => Application.find_by_api_key('api_key').role_ids)
Upvotes: 1