title: How can I access ActiveRecord Associations in class callbacks in rails?
body: <h2>Updated</h2>
  
  <p>Appears to be a precedence error and nothing to do with the question I originally asked.  See discussion below.  </p>
  
  <h2>Original question</h2>
  
  <p>Is it possible to use active record associations in callbacks?  I've tested this code in the console and it works fine as long as it isn't in a callback.  I'm trying to create callbacks that pull attributes from other associated models and I keep getting errors of nil.attribute.</p>
  
  <p>If callbacks are not the correct approach to take, how would one do a similar action in rails?  If the associations are simple, you could use create_association(attributes => ), but as associations get more complex this starts to get messy.  </p>
  
  <p>For example...</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    belongs_to :b
    before_validation_on_create {|user| user.create_b} #note, other logic prevents creating multiple b
  end
  
  class B &lt; ActiveRecord::Base
    has_many :users, :dependent =&gt; destroy
    after_create{ |b| b.create_c }
    has_one :c
  end
  
  class C &lt; ActiveRecord::Base
    belongs_to :b
    after_create :create_alert_email
  
    private
    def create_alert_email 
      self.alert_email = User.find_by_b_id(self.b_id).email  #error, looks for nil.email
    end
  end
  </code></pre>