I have logged-in users who are updating records in the database, and I''d like every model to update the last_modified_by field in the database with the value of the user_id in the session. http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample seemed to be a good starting point, but I''m stuck on how to get at the session''s user_id. Actually, a better question might be the following: How can I pass in a user_id (whether it be from the session, specified in a test, whatever) to be used by all the models when updating records in the database?
I think that your should actually make this requirement before save; that is validates_associated :user And then in your controller or whatever @user = @session[''user''] model.modified_by = @user if model.save ... Benjamin Curtis wrote:>I have logged-in users who are updating records in the database, and I''d >like every model to update the last_modified_by field in the database >with the value of the user_id in the session. >http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample >seemed to be a good starting point, but I''m stuck on how to get at the >session''s user_id. Actually, a better question might be the following: >How can I pass in a user_id (whether it be from the session, specified >in a test, whatever) to be used by all the models when updating records >in the database? >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > > >
I should have mentioned that I''m just getting started with Ruby and with Rails. As a result, I *almost* understand your response. :) Could you elaborate, possibly with a bit more code? Dominic Sisneros wrote:> I think that your should actually make this requirement before save; > that is > > validates_associated :user > > And then in your controller or whatever > > @user = @session[''user''] > model.modified_by = @user > if model.save ... > > Benjamin Curtis wrote: > >> I have logged-in users who are updating records in the database, and I''d >> like every model to update the last_modified_by field in the database >> with the value of the user_id in the session. >> http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample >> seemed to be a good starting point, but I''m stuck on how to get at the >> session''s user_id. Actually, a better question might be the following: >> How can I pass in a user_id (whether it be from the session, specified >> in a test, whatever) to be used by all the models when updating records >> in the database? >> _______________________________________________ >> 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