Reputation: 5953
I have a rails scope statement where I want 2 where clauses. (I will clean up the hardcoding when I've got it working).
This is in my workorder model:
scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232] ) and where("maxsynch = (?)", "N" )
The logic is if the workorder status (wostatus.id) is not one on the values and the workorder.maxsynch is equal to "N"
Upvotes: 2
Views: 4328
Reputation: 3237
Why chaining?
class Workorder < ActiveRecord::Base
scope :laborok, lambda do |ids, maxsynch|
where("wostatus_id NOT IN (?) AND maxsynch = ?", ids, maxsynch)
end
end
Upvotes: 2
Reputation: 33954
What is put into the string is simply passed along to your DB adapter, with the values of the parameters substituted in. So, the simplest form is to just use the AND
keyword:
scope :laborok, where(["wostatus_id NOT IN (?) AND maxsynch = (?)", [231, 230, 8466, 8467, 232], "N"] )
With multiple ?
in the where
string, the first parameter is the string passed to your DB adapter, while subsequent parameters are substituted into the string, in the order provided.
Chaining works okay as well if it's more readable in your specific case:
scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232]).
where("maxsynch = (?)", "N")
Many of the ActiveRecord Query Interface methods can be chained in this way.
Upvotes: 1
Reputation: 54882
you can chain the where() methods:
scope :laborok, where("wostatus_id NOT IN (?)", [1]).where("maxsynch = (?)", "N")
Whatch out, I replaced your array of IDs with [1]
!
Here is the code with your array of IDs:
scope :laborok, where("wostatus_id NOT IN (?)", [231, 230, 8466, 8467, 232]).
where("maxsynch = (?)", "N")
Upvotes: 5