Params

Hi, I’m building a database and I need to save all the changes users do.
This means that every time something goes to the method update, create
or destroy I have to save some information in another table. That’s
fine, the problem is that I need to store all the new information in one
field, and the old information in another.
I tried to do so doing the following:

ModificacionesEmpresa.create(:empresa_id =>@empresa.id, :usuario_id =>
session[:user_id],:tipo => ‘U’, :tupla_nuevo => params[:empresa].to_s,
:tupla_antiguo => @tupla)

The problem is that neither params[:empresa].to_s nor :tupla_antiguo =>
@tupla
are save as readable information… I think I might need to do some
kind of method to get everything in one big string… while I do thata,
Anybody have a smarter solution?

Chika wrote:

The problem is that neither params[:empresa].to_s nor :tupla_antiguo =>
@tupla
are save as readable information… I think I might need to do some
kind of method to get everything in one big string… while I do thata,
Anybody have a smarter solution?

You can turn an object into a big string (in Rails) by using the to_yaml
method. In my app this looks like this:

p = Post.new :permalink=>“http://permalink.com”, :title=>“post title”
=> #<Post:0xb72619a4 @new_record=true,
@attributes={“permalink”=>“http://permalink.com”, “title”=>“post title”,
“blog_id”=>nil, “content”=>nil, “published_at”=>nil, “created_at”=>nil}>

str = p.to_yaml
=> “— !ruby/object:Post \nattributes: \n permalink:
http://permalink.com\n title: post title\n blog_id: \n content: \n
published_at: \n created_at: \nnew_record: true\n”

Then when you need it load it up again use:

YAML::load(str)
=> #<Post:0xb7258aac @new_record=true,
@attributes={“permalink”=>“http://permalink.com”, “title”=>“post title”,
“blog_id”=>nil, “content”=>nil, “published_at”=>nil, “created_at”=>nil}>

Alternatively, instead of using a big string, you might want to consider
using Rick O.'s acts_as_versioned plugin:

On Wed, 2007-01-24 at 16:15 +0100, Chika wrote:

The problem is that neither params[:empresa].to_s nor :tupla_antiguo =>
@tupla
are save as readable information… I think I might need to do some
kind of method to get everything in one big string… while I do thata,
Anybody have a smarter solution?


I think you have to ‘flatten’ a hash to a string.

check out ‘acts_as_audited’ which probably does what you want to do or
will show you the code they use which might give you inspiration.

Craig

Craig W. wrote:

check out ‘acts_as_audited’ which probably does what you want to do or
will show you the code they use which might give you inspiration.

acts_as_audited - Brandon Keepers

Great I did it.
Maybe you know more about act_as_audited and can help me with this.

I have a table “people” that is related wuith the table “area” through
the table “peoplearea”, meaning that each person may have many areas.
The “peoplearea” and the “people” models are being audited, and it
works. The problem is that if I’m editing a person info, and I add or
delete a new area, the “audits” table show this changes as destroying or
creating the table “peoplearea” and I need to be saved as a change made
over the person. Is this possible with this plugin?
Thanks for your help.

Chika wrote:

Hi, I’m building a database and I need to save all the changes users do.
This means that every time something goes to the method update, create
or destroy I have to save some information in another table.

You can also use ActiveRecord::Observer for this. See docs

Regards

Massimo
http://www.addsw.it