English 中文(简体)
未执行主动退约
原标题:ActiveRecord callback is not executed

考虑到以下各点:

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

我确实看不到我做的错误。 这里是失败的光谱产生的“con”产出:

# 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

您可以发现<代码>的原因。 用户标准!

最佳回答

属于“联系违约”(:autosave => real,因此,用户记录在保存参与时就一直存在,因此,积极记录的执行方式只是先界定一个可以挽救用户的传呼。

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.

解决办法是改变现状

before_create :mark_for_invitation, :unless => :user_exists?

a)

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

并将其提交给议会。

http://pivotallabs.com

*** 见http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html“rel=“noreferer”http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html。

问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签