mysql - Rails Joins Query on Multiple Association Search -


i have 2 models user , roles

lets have 3 users "a", "b" , "c" , have 3 roles "x", "y", "z" , relation users has_many roles

lets assume have role x, b have x , y , c have role x

how should modify query b alone have both x , y role

this query returns users either x or y

user.joins(:roles).where("roles.name in (?)", ["x", "y"]) 

i think work on after implement below query, seeing code & implemented.

user.joins("left join roles on roles.name = name").where(["name = 'x', 'y'"]) 

note: not tested


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -