不执行ActiveRecord回调

时间:2011-11-02 23:42:30

标签: ruby-on-rails ruby activerecord callback rspec

鉴于以下规范:

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

我似乎无法通过以下实现传递它:

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

我无法真正看出我做错了什么。这是失败规范的“控制台”输出:

# 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

你能发现为什么没有调用User#invite_user!的问题吗?

1 个答案:

答案 0 :(得分:6)

belongs_to关联默认为:autosave => true,因此在保存Partecipation时会保留用户记录,活动记录实现它的方式是简单地定义保存用户的before_save回调。

由于在before_save回调[1]之后调用before_create回调,因此mark_for_invitation回调被称为&#34;&#34;保存用户关联后,它永远不会被实际执行,因为此时:user_exists?方法始终为true。

解决方案是改变

before_create :mark_for_invitation, :unless => :user_exists?

进入:

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

并将其放在belongs_to

之前

以下文章解释了这一点:http://pivotallabs.com/users/danny/blog/articles/1767-activerecord-callbacks-autosave-before-this-and-that-etc-

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