airblade / paper_trail
- Source
- Commits
- Network (6)
- Issues (4)
- Downloads (9)
- Wiki (2)
- Graphs
-
Branch:
master
click here to add a description
click here to add a homepage
-
usafull for auditing
Comments
-
ActiveRecord::RecordNotSaved thrown, when calling clear on a new record
2 comments Created 3 months ago by 23inhouseIn a case where an association is build (not saved) and then cleared, rails calls destroy or delete.
An exception is thrown - "ActiveRecord::RecordNotSaved: You cannot call create unless the parent is saved".I found adding this fixed it:
def record_destroy
return if new_record? ... end
Maybe it could be added too record_update as well.Here is a case.
class Blog < ActiveRecord::Base
has_paper_trail has_many :posts end
class Post < ActiveRecord::Base
has_paper_trail belongs_to :blog endclass BlogsController < ApplicationController
def action@blog.posts.build @blog.posts.clear #now i dont want it any moreend end
Thanks for the great plugin, i learnt a lot by looking at the code.
Thanks
BenComments
Please log in to comment.Hi Ben,
I can't seem to reproduce your problem. When I call
@blog.posts.buildand then@blog.posts.clearit works fine (Rails 2.3.4). Where is yourRecordNotSavedexception being raised?Thanks,
Andy -
I received the following error when attempting to revert an update:
ArgumentError: syntax error on line 0, col 37: `--- \nreferred_by: Blah\noccupation: !str \n str: electrician\n "@rails_html_safe": false\nname: !str \n str: Abbey Streich\n "@rails_html_safe": false\nfax_number: !str \n str: 978.631.7298 x05457\n "@rails_html_safe": false\ncity: !str \n str: Ipoh\n "@rails_html_safe": false\naddress: !str \n str: 3855 Brekke Bypass\n "@rails_html_safe": false\nzip: !str \n str: "61851"\n "@rails_html_safe": false\nphoto_file_size: 74095\ncreated_at: 2009-12-28 23:02:18 Z\nupdated_at: 2010-01-05 14:58:11 Z\nphoto_file_name: OldWoman.jpg\noutpatient: true\nnotes: !str \n str: ""\n "@rails_html_safe": false\nphoto_content_type: image/jpeg\nid: 54\nuser_id: \nphoto_updated_at: 2009-12-28 15:04:52 Z\nphone_number: !str \n str: 732.727.0168 x9825\n "@rails_html_safe": false\nmale: false\nbirthday: 1889-01-05\nstatus: \nstate: !str \n str: Kuala Lumpur\n "@rails_html_safe": false\nrace: !str \n str: Indian\n "@rails_html_safe": false\n'
from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from /usr/local/lib/ruby/gems/1.8/gems/paper_trail-1.3.1/lib/paper_trail/version.rb:9:in `reify' from (irb):4 from :0Comments
Please log in to comment.This seems to be a problem with the YAML library bundled with Ruby 1.8. You can reproduce this in irb:
>> YAML.load %q{--- \nreferred_by: Blah\noccupation: !str \n str: electrician\n "@_rails_html_safe": false\nname: !str \n str: Abbey Streich\n "@_rails_html_safe": false\nfax_number: !str \n str: 978.631.7298 x05457\n "@_rails_html_safe": false\ncity: !str \n str: Ipoh\n "@_rails_html_safe": false\naddress: !str \n str: 3855 Brekke Bypass\n "@_rails_html_safe": false\nzip: !str \n str: "61851"\n "@_rails_html_safe": false\nphoto_file_size: 74095\ncreated_at: 2009-12-28 23:02:18 Z\nupdated_at: 2010-01-05 14:58:11Z\nphoto_file_name: OldWoman.jpg\noutpatient: true\nnotes: !str \n str: ""\n "@_rails_html_safe": false\nphoto_content_type: image/jpeg\nid: 54\nuser_id: \nphoto_updated_at: 2009-12-28 15:04:52 Z\nphone_number: !str \n str: 732.727.0168 x9825\n "@_rails_html_safe": false\nmale: false\nbirthday: 1889-01-05\nstatus: \nstate: !str \n str: Kuala Lumpur\n "@_rails_html_safe": false\nrace: !str \n str: Indian\n "@_rails_html_safe": false\n} ArgumentError: syntax error on line 0, col 36: `--- \nreferred_by: Blah\noccupation: !str \n str: electrician\n "@_rails_html_safe": false\nname: !str \n str: Abbey Streich\n "@_rails_html_safe": false\nfax_number: !str \n str: 978.631.7298 x05457\n "@_rails_html_safe": false\ncity: !str \n str: Ipoh\n "@_rails_html_safe": false\naddress: !str \n str: 3855 Brekke Bypass\n "@_rails_html_safe": false\nzip: !str \n str: "61851"\n "@_rails_html_safe": false\nphoto_file_size: 74095\ncreated_at: 2009-12-28 23:02:18 Z\nupdated_at: 2010-01-05 14:58:11Z\nphoto_file_name: OldWoman.jpg\noutpatient: true\nnotes: !str \n str: ""\n "@_rails_html_safe": false\nphoto_content_type: image/jpeg\nid: 54\nuser_id: \nphoto_updated_at: 2009-12-28 15:04:52 Z\nphone_number: !str \n str: 732.727.0168 x9825\n "@_rails_html_safe": false\nmale: false\nbirthday: 1889-01-05\nstatus: \nstate: !str \n str: Kuala Lumpur\n "@_rails_html_safe": false\nrace: !str \n str: Indian\n "@_rails_html_safe": false\n' from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load' from (irb):24So it looks like YAML cannot parse its own output in your case. I don't know enough about YAML to fix this, but I'll keep looking around to see if I can find a solution.




Currently the whodunnit field is populated by calling the current_user method on your application controller (or you can set it manually at the model/migration level).
So you could modify your current_user method to return the user's IP as well as anything else you need, though that may not be ideal for you.
Perhaps the method that PaperTrail calls on the controller to find out whodunnit should be configurable? It would default to current_user but you could then specify, say, current_user_and_ip instead. Does that sound good?
it would be great if its configurable, not only to get he ip, i use authlogic and the current_user method must be private.
Ok, I'll make it configurable.
By the way, PaperTrail works with authlogic's private current_user method.
Another idea would be to add some hooks at certain points to the version table objects lifecycle. This would allow for easy user extension of the version table. What do you think Andy?
Do you mean, for example, a before update hook on the version object? It sounds like an interesting idea, Simon. Did you have anything specific in mind? I don't feel I've quite grasped what you're suggesting!
Hi Andy, Sorry for the quick explanation! I think you've got it though, a set of before/after hooks on the version object would allow for 2 possible future scenarios:
1) Addition of user required fields to the Version table (eg. A string representation of what has changed, IP address, phase of moon etc)
2) Possibly open the door for dealing with associations.
Thanks for the details, Simon. So would this sort of thing do?
http://gist.github.com/205881
It absolutely would! :). I guess we could also patch to pass in a proc to the has_paper_trail method to hide the opening up the Version class bit from the user? Thanks!
actually, forget that. It's good to be explicit sometimes.