ruby on rails - Activerecord complex join -


i have problem activerecord join.

i have object "user" follows items, , object "content" has followable items (brands in case) associated.

u , c user , content.

now have these 2 queries:

bf = u.follows.where('followable_type = ?', "brand").map(&:followable_id)  [22, 188, 182, 71, 24, 2]  c.brands.map(&:id)  [2] 

i need obtain array values present in both query, in case [2].

how can write single query?

something like

bf = u.follows.where('followable_type = ?', "brand").where followable_id inside c.brands_ids 

update:

brands = author.follows.where('followable_type = ?', "brand").where(followable_id: content.brands.map(&:id)).map(&:followable).map(&:name).join(', ') 

try using query:

sql_subquery = u.follows.where(followable_type: 'brand').select(:followable_id).to_sql u.follows.where( followable_id: sql_subquery ) 

also, advice, try using complete names variables. makes code more readable, you.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -