Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Original gets overwriten if column is a serialized Array #25

Closed
saifis opened this issue Dec 19, 2010 · 8 comments
Closed

Original gets overwriten if column is a serialized Array #25

saifis opened this issue Dec 19, 2010 · 8 comments

Comments

@saifis
Copy link

saifis commented Dec 19, 2010

Your plugin is working great except for one issue, I have a serialized Array stored inside a model that is being versioned, and any changes done to the Array seems to be going through, when I call model.save or model.update_attribute :Array_column, the original data entry seems to be updated, thus loosing the original data, which is a critical issue in my case. Please help

@airblade
Copy link
Collaborator

Please could you post a short gist / code snippet demonstrating the problem? I.e. what you expect to happen and what actually happens. That'll help me figure out the problem for you.

Thanks,
Andy

@saifis
Copy link
Author

saifis commented Dec 20, 2010

thanks for the reply, I've been looking into the code but was still unable to find a reason.

What I am doing is using the paper_trail gem to create a table that has a serialized array in it.
Model A
serialize :xxx

Controller
a = A.new
a.xxx = {large array}
a.save

--later
b = A.find(a's id)
b.xxx = {edited large array}
b.save

To my understanding this should make 2 versions entries, one as head, which contains nothing, and another that stores the version I made with instance a,
however, when I try loading this version and call noify, the xxx Array is not of the original, but is the edited one. I call the version as so

version = Versions.find( origin_id )
a = version.next.noify

Am I just doing this wrong?

I am sorry to be irresponsible but have become very busy at the moment, and may not be able to commit my efforts adequately, but will try to give all the information needed. I'll try to strip the sensitive info out and make a better example.

@saifis
Copy link
Author

saifis commented Dec 20, 2010

and I've mistakenly pushed to comment and close button....

@airblade
Copy link
Collaborator

It sounds like you're doing it right. When a is created, the version written won't contain anything. When b is saved, the version written will store a's attributes -- just as you said. And it looks like you're getting a array correctly.

I'll look into this for you, but I'm very busy like you and I probably won't get a chance for several days (and then I'm away on holiday for a week)...

@saifis
Copy link
Author

saifis commented Dec 21, 2010

No problem, I'm making you wait, so should you

@saifis
Copy link
Author

saifis commented Dec 23, 2010

I think I found out a way to recreate my problem, the problem was is that the serialized Array I had inside the paper_trailed model was nested and included hashes inside,and the changes inside the hashes do not seem to be recognized as changes.

here is the gist for what I typed in
https://gist.github.com/753029

The reason why the original was getting overwritten was because I had another attribute, which was a normal string inside AAA, which I was updating as well, which would cause a new version to be made, however paper_trail would not notice that aa.foo was changed, and would save over it. thus altering the original.

Now, having such an ugly thing inside a database is truly just poor design, but thats another story.

Hope this helps you.

@airblade
Copy link
Collaborator

airblade commented Jan 5, 2011

PaperTrail uses ActiveModel::Dirty's changed? method to detect whether any attributes have changed. Its documentation says it can't automatically track changes to attributes modified in-place, so for those you have to tell ActiveModel about the change.

I think this is why your modification (aa.foo[0][:cc]["foo"] = "barbar") is not causing a new version to be saved. However I'm not too sure what I can do about this.

@airblade
Copy link
Collaborator

Closing because it's gone quiet. Please re-open if you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants