timpone
timpone

Reputation: 19979

after_create callback not seeming to work correctly

I have the following code with the log info. The global_id value is set to attr_accessible

This one works

code:

Location.update(model[:id],:global_id => gi[:id])

log

before location save
  Location Load (0.3ms)  SELECT `locations`.* FROM `locations` WHERE `locations`.`id` = 280 LIMIT 1
 (0.3ms)  UPDATE `locations` SET `global_id` = 11490, `updated_at` = '2012-02-16 04:48:17' WHERE `locations`.`id` = 280
after location save

This one doesn't and I'm not sure why. Any ideas:

code:

User.update(model[:id],:global_id => gi[:id])

log:

here is more info
 here i am and I am a user
 User Load (0.3ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 31 LIMIT 1
  (0.3ms)  SELECT 1 FROM `users` WHERE (`users`.`email` = BINARY '[email protected]' AND `users`.`id` != 31) LIMIT 1
after my user update

Any ideas why this second one doesn't work?

thx

edit #1 Both models have this:

after_create SaveGlobalInfo

with that class being:

class SaveGlobalInfo < ActiveRecord::Base
  def self.after_create(model)
    gi=GlobalIdentification.create()
    if (model.class.name=='Location')
      puts "before location save"
      Location.update(model[:id],:global_id => gi[:id])
      puts "after location save"
    elsif (model.class.name=='User')
      puts "here i am and I am a user"
      User.update(model[:id],:global_id => gi[:id])
      puts "after my user update"
    end

Upvotes: 2

Views: 2014

Answers (1)

tadman
tadman

Reputation: 211740

If I had to guess it's because you're failing some kind of validation. The test being performed with the second SQL call is to ensure uniqueness on your email field. What if this is failing and the record can't be saved?

This is especially odd:

after_create SaveGlobalInfo

You can create helper classes for callbacks, but I've never seen them as actual ActiveRecord models before. Maybe you mean to define a module?

Upvotes: 3

Related Questions