update: Это тот случай, когда вызов update_attributes получает свою собственную транзакцию?after_commit callback вызывается несколько раз
Я рассмотрел вопрос this и по причинам в дополнение к этому вопросу, я решил пойти с after_commit как правильный крючок. Проблема в том, что он называется множественным (ровно три) раза. Код немного сложно объяснить, но в основном есть модель, которая имеет профиль
include Traits::Blobs::Holder
в holder.rb у меня есть:
module ClassMethods
def belongs_to_blob(name, options = {})
clazz = options[:class_name] ? options[:class_name].constantize : Blob
foreign_key = options[:foreign_key] || :"#{name}_id"
define_method "save_#{name}" do
blob = self.send(name)
if self.errors.any? && blob && blob.valid?
after_transaction do
blob.save!
#self[foreign_key] = blob.id
#save resume anyway
self.update_attribute(foreign_key, blob.id)
end
end
end
after_validation "save_#{name}"
belongs_to name, options
accepts_nested_attributes_for name
end
end
, наконец, в самом profile.rb у меня есть:
after_commit :send_messages_after_registration!
protected
def send_messages_after_registration!
Rails.logger.debug("ENTERED : send_messages_after_registration " + self.owner.email.to_s)
if self.completed?
Rails.logger.debug("completed? is true " + self.owner.email.to_s)
JobSeekerNotifier.webinar_notification(self.owner.id).deliver
Resque.enqueue_in(48.hours, TrackReminderWorker, self.owner.id)
end
end
Кажется, что метод введен 3 раза. Я пытался понять это в течение нескольких дней, поэтому любое руководство, которое вы можете предоставить, будет оценено по достоинству.
кодконтроллера:
def create
@user = Customer.new(params[:customer].merge(
:source => cookies[:source]
))
@user.require_password = true
respond_to do |f|
if @user.save
promote_provisional_user(@user) if cookies[:provisional_user_id]
@user.profile.update_attributes(:firsttime => true, :last_job_title => params[:job_title]) unless params[:job_title].blank?
if params[:resume]
@user.profile.firsttime = true
@user.profile.build_resume(:file => params[:resume])
@user.profile.resume.save
@user.profile.save
end
...
end
Имеет ли запрос профиля own_to_blob? –
Как выглядит код вызова? – klochner
обновил мой вопрос с кодом контроллера – Ramy