Reputation: 4617
I'm working with some old code where major refactoring isn't an option. The following line of code:
Address.joins(:contact => :user).where('users.organization_id = ?', session[:org_id])
produces a list of addresses. Addresses are linked to the Contacts table, which is in turn linked to the Users table.
I need a way to only join on certain users within a certain scope, say User.active or User.inactive.
Is there a way I can do this without major refactors to my database?
Upvotes: 1
Views: 90
Reputation: 1570
Assuming you have a boolean attribute which says whether a user is active or not in your Users table...then you don't need to do anything to your database...you define a scope in the User model to run the line below
User.active.joins(:address => :contacts).where('users.organization_id = ?', session[:blah-blah]')
If you don't have that boolean attribute in your database, then you have to devise a certain mechanism which would work on various attributes to determine whether User is active or not...
Upvotes: 1