# Searching a model based on a filter on its association

**URL:** <https://www.ruby-forum.com/t/searching-a-model-based-on-a-filter-on-its-association/177258>\
**Category:** Rails\
**Created:** [October 14, 2009, 8:18am UTC](https://www.ruby-forum.com/t/searching-a-model-based-on-a-filter-on-its-association/177258 "2009-10-14T08:18:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [October 14, 2009, 8:18am UTC](https://www.ruby-forum.com/t/searching-a-model-based-on-a-filter-on-its-association/177258/1 "2009-10-14T08:18:05Z")

</div>

Hi all,

I got association like the following in my rails app.  
Class User \< ActiveRecord::Base  
has\_one :profile  
end

Class Profile \< ActiveRecord::Base  
establish\_connection :some\_other\_db  
belongs\_to :user  
end

I need to search users based on the age stored in the profile  
I tried using the :joins option of find. but it seems it works only on  
tables in the same db.

Is there a way i can tell (:joins) to join a table from another db? Or  
is  
there some other better way in which this could be achieved?

–  
Sahil D.

---

<div class="post-metadata">

**Author:** ![Sahil\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee7513/32.png) [@Sahil\_D](https://www.ruby-forum.com/u/Sahil_D)\
**Post date:** [October 14, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/searching-a-model-based-on-a-filter-on-its-association/177258/2 "2009-10-14T14:59:19Z")

</div>

Sahil D. wrote:

> I need to search users based on the age stored in the profile  
> I tried using the :joins option of find. but it seems it works only on  
> tables in the same db.

But of course.

So search the Profiles, get your list of user\_id’s that match the  
criteria, then select the Users.
