Topic: Getting model type

I'm trying to create a multi-model observer that performs a before_save(model) to record the past and new state of the model. Obviously, the new model is passed, from which I can get the ID to find the old model. However, how do I find out what model the model is (ie: company, contact, address, etc) to pull that data? Or is there a way to figure out from the passed in model what it was before changes?

Thanks for the help!

Just in case I'm completely confusing, here's a template of the data I'm trying to generate:

PersonA updated the data:
ColumnA: oldValue => newValue
ColumnB: oldValue => newValue
...

Re: Getting model type

Well, all models have an "attributes" method which returns a hash with all the column data. You can use this if you just want an old "snapshot" of the attributes:

old_attributes = model.attributes

If you want to determine the class of a given model you can use the "kind_of?" method:

if model.kind_of? Address
  # model is an address
end

Railscasts - Free Ruby on Rails Screencasts

Re: Getting model type

why not just use class?

if model.class == Address
  # ...
end

Re: Getting model type

That will work too. kind_of method is a little smarter in that it will compare the class inheritance. You can also do a case statement:

case model
when Address
  #...
when Company
  #...
end

Railscasts - Free Ruby on Rails Screencasts