ruby on rails - Clean and concise way to find active records that have the same id as another set of active records -
i have table called shoppers , table called users
. have shopper_id
foreign key in shoppers
table , refers primary key id
in users
table.
i ran query called @shoppers = shoppers.where("some condition")
this allowed me set of shoppers satisfy condition. next select users have same id
shopper_id
individual objects in @shoppers.
i know writing loop, wondering if ruby on rails allows me write users.where
condition can me obtain subset of user objects same id
shopper_id
arranged in ascending order name field in users
table.
any ideas?
try this.
@shoppers = shopper.where("some condition") @users = user.where(id: @shoppers.collect(&:shopper_id)).order('name asc')
Comments
Post a Comment