# Searching via association

**URL:** <https://www.ruby-forum.com/t/searching-via-association/48249>\
**Category:** Rails\
**Created:** [November 18, 2005, 6:08am UTC](https://www.ruby-forum.com/t/searching-via-association/48249 "2005-11-18T06:08:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oksteev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/919ad9/32.png) [@oksteev](https://www.ruby-forum.com/u/oksteev)\
**Post date:** [November 18, 2005, 6:08am UTC](https://www.ruby-forum.com/t/searching-via-association/48249/1 "2005-11-18T06:08:28Z")

</div>

I have a belongs\_to :from, :foreign\_key =\> ‘from\_user\_id’ on my User  
object

Now I want to do a User.find() with the :conditions of from = some user  
id.

Is there anyway to use :conditions without the actual column name  
‘from\_user\_id’, but instead using the association name ‘from’? It seems  
this  
would be the better way of doing it according to DRY, so I dont have to  
duplicate the string ‘from\_user\_id’.

Lemme know

- steve dp

---

<div class="post-metadata">

**Author:** ![oksteev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/919ad9/32.png) [@oksteev](https://www.ruby-forum.com/u/oksteev)\
**Post date:** [November 18, 2005, 5:58pm UTC](https://www.ruby-forum.com/t/searching-via-association/48249/2 "2005-11-18T17:58:49Z")

</div>

try User.find\_by\_from\_user\_id(1)

---

<div class="post-metadata">

**Author:** ![oksteev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/919ad9/32.png) [@oksteev](https://www.ruby-forum.com/u/oksteev)\
**Post date:** [November 18, 2005, 1:38pm UTC](https://www.ruby-forum.com/t/searching-via-association/48249/3 "2005-11-18T13:38:31Z")

</div>

steve dp wrote:

> I have a belongs\_to :from, :foreign\_key =\> ‘from\_user\_id’ on my User object
> 
> Now I want to do a User.find() with the :conditions of from = some user id.
> 
> Is there anyway to use :conditions without the actual column name  
> ‘from\_user\_id’, but instead using the association name ‘from’? It seems this  
> would be the better way of doing it according to DRY, so I dont have to  
> duplicate the string ‘from\_user\_id’.

No, AFAIK, the find method is not currently available on belongs\_to and  
has\_one associations.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![oksteev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/919ad9/32.png) [@oksteev](https://www.ruby-forum.com/u/oksteev)\
**Post date:** [November 20, 2005, 9:43pm UTC](https://www.ruby-forum.com/t/searching-via-association/48249/4 "2005-11-20T21:43:43Z")

</div>

Chris H. \<christopher.k.hall@…\> writes:

> try User.find\_by\_from\_user\_id(1)

Doing it this way is still repeating the string ‘from\_user\_id’, I want  
to be  
able to search using the association name I give in belongs\_to (:from).

I want to do it this way so that if I decide to change the column name  
at some  
point I only have to make one change in the code instead of 10.

Any other suggestions?
