Reputation: 4562
I need to grab all users that have an application. User
is part of my core engine, which is used by many other engines. I'd like to keep User unaware of what is using it, which is why I don't want to add has_many :training_applications
in my User
model.
Here are the classes
module Account
class User < ActiveRecord::Base
end
end
module Training
class TrainingApplication < ActiveRecord::Base
belongs_to :user, class: Account::User
end
end
The following obviously won't work because User
has no concept of TrainingApplication
:
Account::User.joins(:training_application).distinct
Is there an elegant way to return a distinct collection of User
objects that are associated with a TrainingApplication
?
What I landed on as a quick solution is
Account::User.where(id: Training::TrainingApplication.all.pluck(:user_id))
but I'm thinking that there's a better solution.
Upvotes: 0
Views: 22
Reputation: 7744
In case there is no way you can add a has_many :training_applications
association to the User, the following should be suitable solutions:
You could type up a joins string yourself:
t1 = Account::User.table_name
t2 = Training::TrainingApplication.table_name
Account::User.
joins("INNER JOINS #{t2} ON #{t2}.user_id = #{t1}.id").
group("#{t1}.id")
For the sake of variety, let me cover the subquery method as well:
Account::User.where("id IN (SELECT user_id FROM #{t2})")
I would go with the joins
method but I believe both solutions will be faster than your current implementation.
Upvotes: 1