On 11/4/05, Jeremy Evans
<jeremyevans0-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> > ''name'' is a column in the database. I want the
following accessor to be
> > run whenever a record is found. In the stock scaffolding it works for
> the
> > list view but not for the edit view. What am I doing wrong?
> >
> > def name
> > user = User.find(self.user_id)
> > if read_attribute(''name'')
=~/(__)(#{user.username})(__)(.*?)(__)/
> >
read_attribute(''name'').gsub!(/(__)(#{user.username})(__)(.*?)(__)/,''\4'')
> > else
> > read_attribute(''name'')
> > end
> > end
>
> You shouldn''t be finding a User if you already have a User object,
> though I''m not sure why what you did failed. Anyway, try:
>
> def name
> read_attribute(''name'')
=~/(?:__)(?:#{username})(?:__)(.*?)(?:__)/
> ? $1 : read_attribute(''name'')
> end
>
Actually User is another model that this model belongs to. I would prefer to
use @session["username"], but since you can''t access that
from a model as
far as I can tell, I have to do User.find(thismodel.user_id).
But I found a better way to do this anyways. The not very well documented
after_find callback seems to be a better fit.
Chris
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails