Rails after_create callback can't access model's attributes

Posted by tybro0103 on Stack Overflow See other posts from Stack Overflow or by tybro0103
Published on 2010-04-07T16:20:11Z Indexed on 2010/04/07 16:23 UTC
Read the original article Hit count: 363

I can't access my model's attributes in the after_create callback... seems like I should be able to right?

controller:

@dog = Dog.new(:color => 'brown', :gender => 'male')
@dog.user_id = current_user.id
@dog.save

model:

class Dog < ActiveRecord::Base
  def after_create
    logger.debug "[DOG CREATED] color:#{color} gender:#{gender} user:#{user_id}"
  end
end

console: (all seems well)

>>Dog.last
=>#<Dog id: 1, color: "brown", gender: "male", user_id: 1>

log: (wtf!?)

...
[DOG CREATED] color: gender:male user
...

Some of my attributes show up and others don't! oh no! Anyone know what I'm doing wrong? I've always been able to user after_create in such ways in the past.

Note: The actual variable names and values I used were different, but the methods and code are the same.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord