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
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.
try User.find_by_from_user_id(1) On 11/18/05, Mark Reginald James <mrj-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> > 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. > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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?