Rails, ActiveRecord, Squeel: Check if value exists in array -
i have 2 tables: 'actor' & 'role'. each table has column named 'ethnicity'. actor can have 1 ethnicity, role can have 1 or more ethnicities.
so example, let's want find roles match actor1's ethnicity below:
actor1 - ethnicity: 'asian'
role1 - ethnicity: 'asian'
role2 - ethnicity: ['asian','caucasian']
this query:
role.where{(ethnicity.eq my{actor.ethnicity})}
will pull role1, not role2. question how construct query pull role1 role2 in example?
i've tried doing:
role.where{(ethnicity.in my{actor.ethnicity})}
but doesn't seem work.
how this?
the first question ask how storing ethnicity array on role model? serialized column? if may better of going has_many relationship if going asking ethnicity.
it seems problem querying string against value potentially array or string. depending on answer question above solution may different.
one option invert your
role.where{(ethnicity.in my{actor.ethnicity})}
to become
role.where{(actor.ethnicity.in my{ethnicity})}
since array here role's ethnicity, not actor's ethnicity. not 100% familiar squeel answer based on basic logic involved.
Comments
Post a Comment