In addition to created_on and updated_on, I would like to add similar fields for the user that created/updated the record. My question though is how do I get this functionality to happen automatically for all models that implement these fields(I''m assuming before_save() would be the proper way to implement)? Do I need to create a base class of my own that inherits ActiveRecord::Base, and have all of my models inherit from that? Thanks, Steve
Steve V wrote:> In addition to created_on and updated_on, I would like to add similar fields > for the user that created/updated the record. My question though is how do I > get this functionality to happen automatically for all models that implement > these fields(I''m assuming before_save() would be the proper way to > implement)? Do I need to create a base class of my own that inherits > ActiveRecord::Base, and have all of my models inherit from that?Hi Steve. I wrote up a walkthrough: http://article.gmane.org/gmane.comp.lang.ruby.rails/6099 jeremy
Wow, that''s really useful. I wonder if this (and other useful snippets like it) should be posted to some place on the rails site. The wiki? As a howto? Somewhere? On Apr 7, 2005 3:14 PM, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> Steve V wrote: > > In addition to created_on and updated_on, I would like to add similar fields > > for the user that created/updated the record. My question though is how do I > > get this functionality to happen automatically for all models that implement > > these fields(I''m assuming before_save() would be the proper way to > > implement)? Do I need to create a base class of my own that inherits > > ActiveRecord::Base, and have all of my models inherit from that? > > Hi Steve. I wrote up a walkthrough: > http://article.gmane.org/gmane.comp.lang.ruby.rails/6099 > > jeremy > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Shalev NessAiver wrote:> Wow, that''s really useful. > > I wonder if this (and other useful snippets like it) should be posted > to some place on the rails site. The wiki? As a howto? Somewhere?It''s not very well titled, or linked from anywhere, but I did copy that post to the wiki[1] so it could be found/commented/etc. by others. 1. http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample -- Lee
This is really helpful, thanks. Now if the user information is in the session, is there some magic to using it in the module? I have: module AccountTracking def self.append_features(base_class) base_class.before_create { |model| model.Account =@session[''account''] } end end and include AccountTracking But it appears that @session isn''t in the module''s scope. Thanks, Mike Lee O''Mara wrote:> Shalev NessAiver wrote: > >> Wow, that''s really useful. >> >> I wonder if this (and other useful snippets like it) should be posted >> to some place on the rails site. The wiki? As a howto? Somewhere? > > > It''s not very well titled, or linked from anywhere, but I did copy that > post to the wiki[1] so it could be found/commented/etc. by others. > > 1. http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample > > -- > Lee > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
>> Wow, that''s really useful. > >> > >> I wonder if this (and other useful snippets like it) should be posted > >> to some place on the rails site. The wiki? As a howto? Somewhere? > > > > > > It''s not very well titled, or linked from anywhere, but I did copy that > > post to the wiki[1] so it could be found/commented/etc. by others. > > > > 1. http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExampleThe problem with that is, the model shouldn''t know there is a session, or a controller, or anything. What if you run from the console and try to save the model? -- rick http://techno-weenie.net
> http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample > > The problem with that is, the model shouldn''t know there is a session, > or a controller, or anything. What if you run from the console and > try to save the model? >I would agree that the model should not know about the session, or anything tied directly to Rails. However, I would approach it that the models know about a certain context. This context would have to be setup in order for the models to work. Especially in this case where the underlying goal is for auditing. If the changes are made from a console app, I still want to have it recorded in the DB that changes x, y, and z were all performed by the console or whatever. Steve
Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:>> > >> > 1. http://wiki.rubyonrails.com/rails/show/ExtendingActiveRecordExample > > The problem with that is, the model shouldn''t know there is a > session, or a controller, or anything. What if you run from the > console and try to save the model?I''m just dropping names because I''m not actually smart enough to know what I''m talking about. Disclaimer asside, wouldn''t this be a job for the observer? -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org