Help with find

I have the following

class Province < ActiveRecord::Base
has_many :cities
attr_accessible :name, :is_active
end

class City < ActiveRecord::Base
belongs_to :province
attr_accessible :name, :province_id, :is_active
end

now I want to get all the provinces with is_active = true which have
cities with is_active = true

please guide me on how this can be done.

On Mar 19, 7:11am, Quee WM [email protected] wrote:

end

now I want to get all the provinces with is_active = true which have
cities with is_active = true

You should be able to do this using the joins option to find (or in
rails 3 by calling .joins(…)) to join the cities table to the
provinces one. You’ll then be able to write conditions that refer to
both tables

Fred

For slighly more idiomatic Ruby, you could use the attribute name
“active”
rather than “is active”. The resulting query would then look something
like:

Province.joins(:cities).where(:active => true).where(“cities.active =
?”,
true)

On Sat, Mar 19, 2011 at 9:49 AM, Frederick C. <

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs