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
Post a Comment