Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.2ruby-on-rails-3.1

How can i do multiple conditionals?


I have a question using rails

How can i do this code using rails

 select * from policy_vehicles where policy_id
    IN ( SELECT id FROM policies WHERE
    client_id IN (SELECT id FROM clients raz_soc = ?) )

Thanks i will appreciate help


Solution

  • .map(&:id)Use the ActiveRecord where method. You can just pass in an array of values for your SQL IN clause.

    PolicyVehicles.where(:column_a => value_a, :column_b => value_b)
    

    So

    PolicyVehicles.where(:client_id => Client.where(:column => :value).map(&:id) )