Shpigford
Shpigford

Reputation: 25338

How would I turn this in to a scope?

I have Job and Task models where Task has_many :jobs and Job belongs_to :task.

When querying jobs, I end up doing a join on task and doing a where like this:

jobs.joins(:task).where('tasks.department = ? and tasks.number = ?', 'PW', '135')

So how could I add a scope with arguments to the Job model that can pull that off?

Upvotes: 0

Views: 27

Answers (1)

veritas1
veritas1

Reputation: 9170

Try:

scope :scope_name, ->(arg1, arg2){ joins(:task).where('tasks.department = ? and tasks.number = ?', arg1, arg2)}

Upvotes: 1

Related Questions