nested forms - how to use before/after destroy callback ?

Javix
2013-03-14T19:12:37Z

How is it possible to use a callback and which one to avoid the destroying
of the last association.
For example, there are 3 following models:

#timesheet.rb
class Timesheet < ActiveRecord::Base
  has_many :activities, dependent: :destroy
  has_many :time_entries, through: :activities
  accepts_nested_attributes_for :activities, allow_destroy: true
end

#activity.rb
class Activity < ActiveRecord::Base
  has_many :time_entries, order: :workdate, dependent: :destroy
  accepts_nested_attributes_for :time_entries, allow_destroy: true,
reject_if: proc { |a| a[:worktime].blank? }
end

#time_entry.rb
class TimeEntry < ActiveRecord::Base
  belongs_to :activity
  validates :worktime, presence: true, inclusion: { in: [0.5, 1] }
end

Every timesheet is created for 7 days (tile_entries) for one activity in
the the same form. To delete I use the technic with jQuery explained at
Railscats:
#add_remove_fields.js

function remove_fields(link) {
  $(link).prev("input[type=hidden]").val("1");
  $(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
  var new_id = new Date().getTime();
  var regexp = new RegExp("new_" + association, "g");
  $(link).parent().before(content.replace(regexp, new_id));
}

#application_helper.rb

def link_to_remove_fields(name, f)
    f.hidden_field(:_destroy) + link_to_function(name,
"remove_fields(this)")
  end

  def link_to_add_fields(name, f, association, timesheet)
...
end

I tried several ways with after_destroy hook, - it didn''t work.
Any idea ? Thanks and regards.

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit