krn
krn

Reputation: 6815

Rails active record: update a list of records

I have two tables: 'countries' and 'airports'. I want to set all the countries, which have at least one airport, as 'active'. This does not work:

  def self.activate_by_airports
    update_all('active = 1', ['country_code IN (?)', Airport.select('DISTINCT(country_code)').where(:active => 1)])
  end

As I understand, Airport.select(...) returns a list of Airport objects, when I need a list of country codes.

What is the right syntax in this case?

Upvotes: 0

Views: 916

Answers (1)

cristian
cristian

Reputation: 8744

Use Airport.select('DISTINCT(country_code)').where(:active => 1).toArray or Airport.select('DISTINCT(country_code)').where(:active => 1).all - Rails 3 only

Upvotes: 2

Related Questions