Forum: Ruby on Rails searching via association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd4f9ba2512f984481b5b00279cee69a?d=identicon&s=25 oksteev (Guest)
on 2005-11-18 06:08
(Received via mailing list)
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
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 mrj (Guest)
on 2005-11-18 13:38
(Received via mailing list)
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.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 christopher.k.hall (Guest)
on 2005-11-18 17:58
(Received via mailing list)
try User.find_by_from_user_id(1)
Cd4f9ba2512f984481b5b00279cee69a?d=identicon&s=25 oksteev (Guest)
on 2005-11-20 21:43
(Received via mailing list)
Chris Hall <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?
This topic is locked and can not be replied to.