Rails[PaperTrail]: accepts_nested_attributes_for not restored -


i'm using papertrail gem. have 3 models faq, subgroup, , group. group has one-to-many relationship subgroup, , subgroup had has-and-belongs-to-many relationship. papertrail doesn't support habtm changed has-many-through. when destroy faq reverted correct subgroup associations.

group on other hand accepts_nested_attributes_for :subgroups , seems why fails. subgroup gets destroyed whenever parent group gets destroyed; papertrail won't restore subgroup when parent group restored. however, can see there versions subgroup, papertrail isn't getting called restore them. how can so?

group.rb

class group < activerecord::base     has_many :subgroups, :dependent => :destroy, inverse_of: :group     validates :name, presence: true     #validates :subgroups, length: { minimum: 1 }     accepts_nested_attributes_for :subgroups, :allow_destroy => true     has_paper_trail end 

subgroup.rb

class subgroup < activerecord::base     belongs_to :group, inverse_of: :subgroups     has_many :faqs_subgroups     has_many :faqs, through: :faqs_subgroups     validates :name, presence: true     validates_presence_of :group     has_paper_trail end 

versions_controller.rb

class versionscontroller < applicationcontroller     def revert         @version = papertrail::version.where(:id => params[:id], :item_type => params[:item_type], :item_id => params[:item_id])[0]          if params[:item_type] == "group"             if @version.reify(:has_many => true)                 @version.reify(:has_many => true).save!             else                  @version.item.destroy             end         else             if @version.reify                 @version.reify.save!             else                 @version.item.destroy             end         end          link_name = params[:act] == "undo" ? "redo" : "undo"         link = view_context.link_to(link_name, revert_version_path(@version.next, :item_type => params[:item_type], :item_id => params[:item_id], :act => "redo"), :method => :post)          if @version.event == "create" && params[:item_type] == "faq"             redirect_to faqs_path, :notice => "successfully undid #{@version.event}. #{link}"         elsif @version.event == "create" && params[:item_type] == "group"             redirect_to groups_path,  :notice => "successfully undid #{@version.event}. #{link}"         else             redirect_to :back, :notice => "successfully undid #{@version.event}. #{link}"         end     end end 

undo_link method in groups_controller.rb

def undo_link   view_context.link_to("undo", revert_version_path(@group.versions.last, :item_type => "group", :item_id => @group.id, :act => "undo"), :method => :post) end 

if papertrail not support reverting subgroup, thinking saving ids , version ids of subgroups group has, , perhaps calling papertrail restore when parent group restored. what's plausible way of doing this?

thanks!

edit forgot add have seen this question not have problem destroying, restoring.

current solution: before destroying group, save ids of subgroups. then, pass array revert_version_path. after reify.save! on group, call revert-hm method in versions_controller manually reify.save subgroups.

revert_hm in versions_controller.rb

def revert_hm(item_type, item_id)     @version = papertrail::version.where(:item_type => item_type, :item_id => item_id).last     if @version.reify != nil         @version.reify.save!     else         @version.item.destroy     end end 

i don't save version id of subgroups because have not been destroyed yet, in destroyed version latest version want reify.save. can used :tag_list of acts_taggable_on though warning cannot papertrail make new versions when associations or tags changed.

it's messy solution if has better 1 please so.

p.s. if using elasticsearch have call model.import on model after calling method. otherwise, elasticsearch think model doesn't have these associations.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -