sql - Rails MySQL query using where statement -
i have users:
name _____ role_________status
user1 -------- role1 ---------------- true
user2 -------- role2 ---------------- true
user3 -------- role2 ---------------- false
user4 -------- role3 ---------------- false
user5 -------- role4 ---------------- true
i have query:
user.where("role = ? or role = ? or role = ? , status = ?", "role1", "role2", "role3", true)
and expect data retrieve is:
user1
user2
but system retrieve user3 if status false.
i try change and
or
retrieves all.
what's wrong query?
what's wrong query lack of parentheses around 'or groups'
user.where("(role = ? or role = ? or role = ?) , status = ?", "role1", "role2", "role3", true)
however, stated in answer, can done in easier way this.
user.where(status: true).where(role: %w{role1 role2 role3})
Comments
Post a Comment