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

Popular posts from this blog

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -

javascript - Using jquery append to add option values into a select element not working -

javascript - Restarting Supervisor and effect on FlaskSocketIO -