Searching via association

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

try User.find_by_from_user_id(1)

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.

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?