# Help with find

**URL:** <https://www.ruby-forum.com/t/help-with-find/204707>\
**Category:** Rails\
**Created:** [March 19, 2011, 8:11am UTC](https://www.ruby-forum.com/t/help-with-find/204707 "2011-03-19T08:11:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 19, 2011, 8:11am UTC](https://www.ruby-forum.com/t/help-with-find/204707/1 "2011-03-19T08:11:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [March 19, 2011, 9:51am UTC](https://www.ruby-forum.com/t/help-with-find/204707/2 "2011-03-19T09:51:30Z")

</div>

On Mar 19, 7:11am, Quee WM [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![qaswm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/5f8ce5/32.png) [@qaswm](https://www.ruby-forum.com/u/qaswm)\
**Post date:** [March 19, 2011, 10:50am UTC](https://www.ruby-forum.com/t/help-with-find/204707/3 "2011-03-19T10:50:54Z")

</div>

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. \<
