I have the code like below throughout my controllers. This is to filter the models for a particular account (for multitenancy). Is there an easy way to DRY this up? The current_account_id is a controller helper method which depends on the current user.
Job.with_account(current_account_id).active.......
Contact.with_account(current_account_id).active.......
Not sure if adding a helper method in Application controller is the best way.
Edit:
To clarify, I could use some code like below:
def job_with_current_account
Job.with_account(current_account_id)
end
and then use this method everywhere in the controller e.g.
job_with_current_account.active.....
I was wondering if there was another, more elegant way to do this.
If you have defined the associations in both directions, and assuming you have a
current_accounthelper, then you could use this instead:It’s a bit more concise, and you don’t need to define the
with_accountscope in all of your models.