Tracking Who Created What

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?

Ryan B. (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!

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.

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 B. (Radar)” [email protected]

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.

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

Ryan B. 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

Roy P. 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

Hi 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

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

Oh cool–good stuff in there it looks like. Thanks!

-Roy