I have many tables in my database and I would like to track which user created which object. I know I could use polymorphic relationships between the tables, but I''m not sure of what goes where. Any help? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar) wrote:> I have many tables in my database and I would like to track which user > created which object. I know I could use polymorphic relationships > between the tables, but I''m not sure of what goes where. Any help?How about a CreatorTable: t.column :user_id, :integer t.column :type, :string t.column :record_id, :integer Then derive FooCreatorTable, BarCreatorTable, etc. Then to find who created Foo 42, you use FooCreatorTable.find_by_record_id(42) My idea might be a solution in search of a problem, though! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I was thinking something like a properties table with the following: user_id entity_type entity_id That I''ve got down pat. I just need to know how to define the relationships in ActiveRecord so I can do stuff like: e = Employer.find(:first) u = User.find(:first) e.created_by = u e.save and then it registers that in the properties table. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I figured it would just be easy to add a belongs_to :owner, :class_name => "User" on the model of each object, and then an owner_id field on all the tables instead of trying to find a "simple" work around. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
For multiple tables polymorphism can be used for a Creator table; and use after_create hook to add the creator records. On Jun 24, 10:02 am, "Ryan Bigg (Radar)" <radarliste...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I figured it would just be easy to add a belongs_to :owner, :class_name => > "User" on the model of each object, and then an owner_id field on all the > tables instead of trying to find a "simple" work around.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> I figured it would just be easy to add a belongs_to :owner, :class_name > => > "User" on the model of each object, and then an owner_id field on all > the > tables instead of trying to find a "simple" work around.I use simple created_by and updated_by fields on each table populated with current_user.login from restful_authentication -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh good--I was hoping someone would reply with a simple method for doing this. So--followup question. I''m guessing that you can''t populate these fields in an ActiveRecord callback (before_save) since current_user is a session (and therefore controller) thing. Is that right? If so--what''s the most efficient way of populating those fields? Imagine you''ve got say 11 different models that all have those attributes--is there anything cool and DRY and meta-programmy you can do to make sure those are always populated? Thanks! -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Ar Chron Sent: Tuesday, June 24, 2008 8:57 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Tracking Who Created What Ryan Bigg wrote:> I figured it would just be easy to add a belongs_to :owner, > :class_name => "User" on the model of each object, and then an > owner_id field on all the tables instead of trying to find a "simple" > work around.I use simple created_by and updated_by fields on each table populated with current_user.login from restful_authentication -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan, Just a reminder [and also for those following along who might not know as well], if you use the association proxy constructors like current_user.foos.build in your controller methods instead of building a Foo and then associating it with the current_user, Rails will take care of it all for you. RSL On Tue, Jun 24, 2008 at 1:29 PM, Pardee, Roy <pardee.r-go57ItdSaco@public.gmane.org> wrote:> > Oh good--I was hoping someone would reply with a simple method for doing > this. > > So--followup question. I''m guessing that you can''t populate these > fields in an ActiveRecord callback (before_save) since current_user is a > session (and therefore controller) thing. Is that right? If so--what''s > the most efficient way of populating those fields? Imagine you''ve got > say 11 different models that all have those attributes--is there > anything cool and DRY and meta-programmy you can do to make sure those > are always populated? > > Thanks! > > -Roy > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Ar Chron > Sent: Tuesday, June 24, 2008 8:57 AM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Tracking Who Created What > > > Ryan Bigg wrote: > > I figured it would just be easy to add a belongs_to :owner, > > :class_name => "User" on the model of each object, and then an > > owner_id field on all the tables instead of trying to find a "simple" > > work around. > > I use simple created_by and updated_by fields on each table populated > with current_user.login from restful_authentication > -- > Posted via http://www.ruby-forum.com/. > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Roy Pardee wrote:> Oh good--I was hoping someone would reply with a simple method for doing > this. > > So--followup question. I''m guessing that you can''t populate these > fields in an ActiveRecord callback (before_save) since current_user is a > session (and therefore controller) thing. Is that right? If so--what''s > the most efficient way of populating those fields? Imagine you''ve got > say 11 different models that all have those attributes--is there > anything cool and DRY and meta-programmy you can do to make sure those > are always populated? > > Thanks! > > -RoyHi Roy, There was a discussion about this not too long ago in this thread http://www.ruby-forum.com/topic/154820 Maybe that will give you some ideas. Peace, Phillip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh cool--good stuff in there it looks like. Thanks! -Roy -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Phillip Koebbe Sent: Wednesday, June 25, 2008 6:27 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Tracking Who Created What Roy Pardee wrote:> Oh good--I was hoping someone would reply with a simple method for > doing this. > > So--followup question. I''m guessing that you can''t populate these > fields in an ActiveRecord callback (before_save) since current_user is> a session (and therefore controller) thing. Is that right? If > so--what''s the most efficient way of populating those fields? Imagine> you''ve got say 11 different models that all have those attributes--is > there anything cool and DRY and meta-programmy you can do to make sure> those are always populated? > > Thanks! > > -RoyHi Roy, There was a discussion about this not too long ago in this thread http://www.ruby-forum.com/topic/154820 Maybe that will give you some ideas. Peace, Phillip -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---