ruby on rails - activerecord exists subquery -


i have following 2 models:

class client < activerecord::base   has_many :orders end  class order < activerecord::base   belongs_to :client end 

i want query clients orders specified in list (order_1, order_2), at same time, client's orders needed. can sql below:

select * clients c join orders o on c.id = o.client_id exists   (select *   clients c1   join orders o1   on c1.id = o1.client_id   o1.id in ('order_1', 'order_2')   , c1.id = c.id   ); 

is there way in rails way? following code give satisfied clients, client.orders returns specified orders.

clients.includes(:orders).where(orders: { id: ['order_1', 'order_2'] }) 

i don't know how information in 1 query.

i know years later, here's how (based on this blog post).

order.where(client_id:   client.joins(:orders)     .where(orders: {id: ['order_1', 'order_2']})     .select(:id) ) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -