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

Can not store meta data on create operation, but creating being update operation in model. #185

Closed
rokibulhassan opened this issue Dec 24, 2012 · 5 comments

Comments

@rokibulhassan
Copy link

I have something like following in my model but data not set to hotel_id and agent_user_id while creating the object. Can not figure it out. I have hotel_id and agent_user_id in my version table. I also use attr_accessible :hotel_id, :agent_user_id in paper_trail.rb

class Hotel < ActiveRecord::Base
  has_paper_trail :ignore => [:user_id],  :meta => {:hotel_id => :id, :agent_user_id => :user_id}
  belongs_to :user
end

what is happening here?

@batter
Copy link
Collaborator

batter commented Dec 31, 2012

Note, that on an initial create action, the metadata columns will be nil due to the fact that the metadata columns get filled with the data that matches that of the object that is returned by reify. So it would be expected behavior for those columns to be nil on a create, but if you subsequently call update_attributes on one of the other columns besides user_id, the metadata columns will get set with values (assuming values exist on the Hotel object).

If you want the metadata to be filled on create actions as well, then please change the has_paper_trail declaration accordingly:

has_paper_trail :ignore => [:user_id],
  :meta => {
    :hotel_id => Proc.new { |hotel| hotel.id },
    :agent_user_id => Proc.new { |hotel| hotel.user_id }
  }

@rokibulhassan
Copy link
Author

Following code block working for me,

has_paper_trail :ignore => [:user_id], :meta => {:hotel_id => Proc.new { |hotel| hotel.id }, :agent_user_id => Proc.new { |hotel| hotel.user_id } }

thank a lot 👍

@decasia
Copy link

decasia commented Aug 26, 2013

Thanks for this thread, folks, very helpful. It wouldn't hurt for this howto to be added to the README too: I think it comes in handy when you are doing something like the following very common case:

I have a parent object A and a set of child objects B, C, and D. I want to be able to readily view all versions of A and its child objects, by selecting all the versions where a metadata column matches A's id. But when I create a new child object E, its metadata column doesn't get populated by the meta: {parent_id: parent_id} code's described in the docs...

Anyway, the fix is easy enough to find in this thread, so I guess anyone motivated can look it up like I just did.

@batter
Copy link
Collaborator

batter commented Aug 26, 2013

@decasia - In terms of restoring relationships when reifying, there is already some support for restoring has_one associations (as mentioned in the README), and I'm hoping to try to add functionality for restoring has_many associations into the 3.0.0 release, but haven't made a lot of progress yet. Contributions or suggestions are welcome, see #148, #44, #12, etc.

If you have a suggestion for how the above information can be explained clearly and succinctly in a helpful way on the README, please make a pull request.

@betjaminrichards
Copy link

+1 to add this to the readme.

@batter batter changed the title Can not store meta data on create operation, but crating being update operation in model. Can not store meta data on create operation, but creating being update operation in model. Jan 7, 2015
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

4 participants