I''m creating a little app at the minute and i''d like to stamp each record with who created/updated it in addition to the timestamp. I cant find a ''magic'' field to do this and was wondering how best to achieve it in a similar manner to the way the timestamping is done. I used the salted_login generator to create my user managment if thats of any help Mike
Obviously this can''t be automatic because your model can''t know anything about the application it runs in. ( Model has to work well from console as well as possibly from a gui app or from web services ). The best way is to open up ActiveRecord::Base again in your application and add a method called save_as_user which might looks something like this (untested) class ActiveRecord::Base include UserManagement end module UserManagement def save_as_user(u) if new_record? self.created_by = u if self.respond_to?(:created_by) end self.updated_by = u if self.respond_to?(:updated_by) save end end now you can use .save_as_user on all your active record objects. On 5/11/05, Mike Gilbert <mike-jLbnyU+aYbUAvxtiuMwx3w@public.gmane.org> wrote:> I''m creating a little app at the minute and i''d like to stamp each > record with who created/updated > it in addition to the timestamp. I cant find a ''magic'' field to do this > and was wondering how best to > achieve it in a similar manner to the way the timestamping is done. > > I used the salted_login generator to create my user managment if thats > of any help > > Mike > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
Some persistence layers (such as Hibernate for Java) can automatically version rows, to allow for handling dirty-reads. As such I can''t think of a major technical problem with ActiveRecord handling this in some automatic fashion, where you perhaps specify a column and a bit of Ruby to generate the stamp. You could take this further, and enable automatic generation of AuditRecords for every SQL action if you wanted, although that''s probably a discussion for another time... sam On 5/11/05, Tobias Luetke <tobias.luetke-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Obviously this can''t be automatic because your model can''t know > anything about the application it runs in. ( Model has to work well > from console as well as possibly from a gui app or from web services > ). > > The best way is to open up ActiveRecord::Base again in your > application and add a method called save_as_user which might looks > something like this (untested) > > class ActiveRecord::Base > include UserManagement > end > > module UserManagement > def save_as_user(u) > if new_record? > self.created_by = u if self.respond_to?(:created_by) > end > self.updated_by = u if self.respond_to?(:updated_by) > save > end > end > > now you can use .save_as_user on all your active record objects. > > On 5/11/05, Mike Gilbert <mike-jLbnyU+aYbUAvxtiuMwx3w@public.gmane.org> wrote: > > I''m creating a little app at the minute and i''d like to stamp each > > record with who created/updated > > it in addition to the timestamp. I cant find a ''magic'' field to do this > > and was wondering how best to > > achieve it in a similar manner to the way the timestamping is done. > > > > I used the salted_login generator to create my user managment if thats > > of any help > > > > Mike > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > Tobi > http://www.snowdevil.ca - Snowboards that don''t suck > http://www.hieraki.org - Open source book authoring > http://blog.leetsoft.com - Technical weblog > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- sam http://www.magpiebrain.com/
Thanks Tobias Tobias Luetke wrote:>Obviously this can''t be automatic because your model can''t know >anything about the application it runs in. ( Model has to work well >from console as well as possibly from a gui app or from web services >). > >The best way is to open up ActiveRecord::Base again in your >application and add a method called save_as_user which might looks >something like this (untested) > >class ActiveRecord::Base > include UserManagement >end > >module UserManagement > def save_as_user(u) > if new_record? > self.created_by = u if self.respond_to?(:created_by) > end > self.updated_by = u if self.respond_to?(:updated_by) > save > end >end > >now you can use .save_as_user on all your active record objects. > >On 5/11/05, Mike Gilbert <mike-jLbnyU+aYbUAvxtiuMwx3w@public.gmane.org> wrote: > > >>I''m creating a little app at the minute and i''d like to stamp each >>record with who created/updated >>it in addition to the timestamp. I cant find a ''magic'' field to do this >>and was wondering how best to >>achieve it in a similar manner to the way the timestamping is done. >> >>I used the salted_login generator to create my user managment if thats >>of any help >> >>Mike >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > > >
On May 11, 2005, at 4:17 AM, Sam Newman wrote:> Some persistence layers (such as Hibernate for Java) can automatically > version rows, to allow for handling dirty-reads. As such I can''t think > of a major technical problem with ActiveRecord handling this in some > automatic fashion, where you perhaps specify a column and a bit of > Ruby to generate the stamp. You could take this further, and enable > automatic generation of AuditRecords for every SQL action if you > wanted, although that''s probably a discussion for another time...Obviously I could spelunk the Hibernate site, but could you provide a pointer and perhaps a summary of how Hibernate does versioning? I''m very intrigued to learn how they do it. The application I work on for "The Man" uses a lot of versioning and learning it often makes new hires weep. I wonder if Hibernate''s scheme is more or less complex. Thanks, -- ~akk http://therealadam.com
There may be other ways to configure it on hibernate, but for us, here is how it works. Every class that you want to version has an int version attribute, and likewise a column. Whenever you modify the object, the version number is incremented. The update statement is written such that if the version is not what it was when loaded, it will throw a "stale object" exception. David On Wednesday 11 May 2005 07:20 am, Adam Keys wrote:> On May 11, 2005, at 4:17 AM, Sam Newman wrote: > > Some persistence layers (such as Hibernate for Java) can automatically > > version rows, to allow for handling dirty-reads. As such I can''t think > > of a major technical problem with ActiveRecord handling this in some > > automatic fashion, where you perhaps specify a column and a bit of > > Ruby to generate the stamp. You could take this further, and enable > > automatic generation of AuditRecords for every SQL action if you > > wanted, although that''s probably a discussion for another time... > > Obviously I could spelunk the Hibernate site, but could you provide a > pointer and perhaps a summary of how Hibernate does versioning? I''m > very intrigued to learn how they do it. The application I work on for > "The Man" uses a lot of versioning and learning it often makes new > hires weep. I wonder if Hibernate''s scheme is more or less complex. > > Thanks, > -- > ~akk > http://therealadam.com > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hmm, sounds familiar... http://rails.rubyonrails.com/classes/ActiveRecord/Locking.html On 5/11/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote:> There may be other ways to configure it on hibernate, but for us, here is how > it works. Every class that you want to version has an int version attribute, > and likewise a column. Whenever you modify the object, the version number is > incremented. The update statement is written such that if the version is not > what it was when loaded, it will throw a "stale object" exception. > > David > > On Wednesday 11 May 2005 07:20 am, Adam Keys wrote: > > On May 11, 2005, at 4:17 AM, Sam Newman wrote: > > > Some persistence layers (such as Hibernate for Java) can automatically > > > version rows, to allow for handling dirty-reads. As such I can''t think > > > of a major technical problem with ActiveRecord handling this in some > > > automatic fashion, where you perhaps specify a column and a bit of > > > Ruby to generate the stamp. You could take this further, and enable > > > automatic generation of AuditRecords for every SQL action if you > > > wanted, although that''s probably a discussion for another time... > > > > Obviously I could spelunk the Hibernate site, but could you provide a > > pointer and perhaps a summary of how Hibernate does versioning? I''m > > very intrigued to learn how they do it. The application I work on for > > "The Man" uses a lot of versioning and learning it often makes new > > hires weep. I wonder if Hibernate''s scheme is more or less complex. > > > > Thanks, > > -- > > ~akk > > http://therealadam.com > > > > _______________________________________________ > > 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 >-- rick http://techno-weenie.net
It does indeed - hadn''t seen that, thanks Rick. Hibernate certainly also supports the use of a timestamp for versioning as an alternate technique for locking - and I can''t see why ActiveRecord couldn''t take a custom handler for versioning which could use a timestamp + username or whatever. sam On 5/12/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmm, sounds familiar... > > http://rails.rubyonrails.com/classes/ActiveRecord/Locking.html > > On 5/11/05, David Corbin <dcorbin-wmGZ+vDKSyrZJqsBc5GL+g@public.gmane.org> wrote: > > There may be other ways to configure it on hibernate, but for us, here is how > > it works. Every class that you want to version has an int version attribute, > > and likewise a column. Whenever you modify the object, the version number is > > incremented. The update statement is written such that if the version is not > > what it was when loaded, it will throw a "stale object" exception. > > > > David > >
On Thursday 12 May 2005 03:53 am, Sam Newman wrote:> It does indeed - hadn''t seen that, thanks Rick. > > Hibernate certainly also supports the use of a timestamp for > versioning as an alternate technique for locking - and I can''t see why > ActiveRecord couldn''t take a custom handler for versioning which could > use a timestamp + username or whatever. >Using a timestamp for versioning is a bad idea. Most systems/languages today only have millisecond accuracy or worse. That means it''s possible to processes to update the record with the same timestamp that it had to start with, so you can''t tell there was a change. Furhtermore, with multiple systems (say you have a fastcgi cluster), you''d have to keep the system times VERY tightly synchronized. David