Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24118

ActiveRecord callback is not executed

Given the following spec:

describe Participation do
  describe "invitation by email", :focus do
    let(:participation) { build :participation } # Factory
    let(:email)         { participation.email }

    it "should send an invitation" do
      # This one is failing
      binding.pry # The code below is executed here
      participation.should_receive(:invite_user!)
      participation.save!
    end

    context "when user already exists" do
      let!(:existing) { create :user, :email => email }
      it "should not send an invitation" do
        participation.should_not_receive(:invite_user!)
        participation.save!
      end
    end
  end
end

I can't seem to pass it with the following implementation:

class Participation < ActiveRecord::Base
  attr_accessor :email

  belongs_to :user
  validates  :email, :email => true, :on => :create, :if => :using_email?

  before_validation :set_user_by_email,   :if     => :using_email?, :on => :create
  before_create     :mark_for_invitation, :unless => :user_exists?
  after_create      :invite_user!,        :if     => :marked_for_invitation?


  def using_email?
    email.present?
  end

  def user_exists?
    user.present? and user.persisted?
  end

  def set_user_by_email
    self.user = User.find_by_email(email)
    self.user ||= User.new(email: email).tap do |u|
      u.status = :invited
    end
  end

  def mark_for_invitation
    @invite_user = true
    true # make sure not cancelling the callback chain
  end

  def marked_for_invitation?
    !!@invite_user
  end

  def invite_user!
    # TODO: Send the invitation email or something
  end
end

I can't really see what I am doing wrong. Here is the "console" output from the failing spec:

# Check the before_validation callback options:
participation.user # nil
participation.valid? # true
participation.user # User{id: nil}

# Check the before_create callback options:
participation.user_exists? # false
participation.mark_for_invitation # true

# Check the after_create callback options:
participation.marked_for_invitation? # true

# After all this I expect the "invite_user!" to be called:
participation.stub(:invite_user!) { puts "Doesn't get called :(" }
participation.save! # => true, Nothing is printed, which is consistent with the spec
participation.user_id # => 11, so the user has been saved

Can you spot the issue why the User#invite_user! is not called?

Upvotes: 2

Views: 2639

Answers (1)

LucaM
LucaM

Reputation: 2846

The belongs_to association defaults to :autosave => true, therefore the User record gets persisted when the Partecipation is saved, the way active record implements it is by simply defining a before_save callback that saves the user.

Since the before_create callbacks are called after the before_save callbacks [1] the mark_for_invitation callback is "called" after the user association is saved, thus it is never actually executed as the :user_exists? method is always true at that point.

The solution is to change the

before_create :mark_for_invitation, :unless => :user_exists?

into a:

before_save :mark_for_invitation, :on=>:create, :unless => :user_exists?

and put it before the belongs_to

Here is an article that explains this: http://pivotallabs.com/users/danny/blog/articles/1767-activerecord-callbacks-autosave-before-this-and-that-etc-

[1] see: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Upvotes: 7

Related Questions