Frage

I'm probably misunderstanding something here.

I have a model Secondant that I create with Fabrication in my model rspec.

main_user = Fabricate :user, email: TESTEMAIL
sec = Fabricate :secondant, email: SECEMAIL, user_id: main_user.id
sec_user = Fabricate :user, email: SECEMAIL
ActionMailer::Base.deliveries = []
debugger

At this point when I look at the value of secondant_id in the sec model, the attribute is empty (it get's filled in a after_create callback). When i retrieve the model just created from the database that attribute is filled. Why are those two not in sync?

   27        main_user = Fabricate :user, email: TESTEMAIL
   28        sec = Fabricate :secondant, email: SECEMAIL, user_id: main_user.id
   29        sec_user = Fabricate :user, email: SECEMAIL
   30        ActionMailer::Base.deliveries = []
   31        debugger
=> 32        sec.destroy
   33      end
   34
   35      it 'should have a secondant_id assigned' do
   36        sec.secondant_id.should_not be_nil
(rdb:1) e sec
#<Secondant id: 519, user_id: 1095, email: "secondant@hotmail.com", secondant_id: nil, created_at: "2013-10-10 13:13:29", updated_at: "2013-10-10 13:13:29", reported: false>
(rdb:1) e Secondant.where(id: sec.id).first
#<Secondant id: 519, user_id: 1095, email: "secondant@hotmail.com", secondant_id: 1096, created_at: "2013-10-10 13:13:29", updated_at: "2013-10-10 13:13:29", reported: false>

My after_create callback:

  def find_user
    user = User.where(email: self.email).first

    if user
      # create the link to the user
      self.secondant_id = user.id
      self.save

      # see if this is the second one
      if Secondant.where('user_id = ? and secondant_id is not null', user_id).count == 2
        user.do_somthing
      end
    end

    return
  end

EDIT

There is a similar callback in the user class, which is firing in this case (thanks Peter)

  def find_secondant
    Secondant.where(email: email).find_each do |sec|
      sec.secondant_id = id
      sec.save
    end
  end
War es hilfreich?

Lösung

At the time you create sec, the user with the identical email has not been created, so your after_save callback should not be setting secondant_id.

I can only assume that your find_user method is getting invoked as a result of the User creation or the where operation you are executing in the debugger, resulting in the secondant_id field being set at that time. It won't be reflected in sec unless/until you do a reload, as the Ruby object created by where is distinct from the sec Ruby object.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top