Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three ways to do this that I've found, none of them satisfactory but at least two of them will work reliably, even if they're surprising in design and can create some very subtle bugs down the road.</p> <h3>A) Return if an Attribute You'll Use is Missing</h3> <pre class="lang-rb prettyprint-override"><code>def after_find # What a flaky way to ruin your code: return unless self.has_attribute?(:some_attrib_your_model_uses_in_after_find) @amazing_pain = @some_attrib_your_model_uses_in_after_find + 1 end </code></pre> <h3>B) Grep the Entire Callstack</h3> <p><i>(and pray exists? is in there because you think it is)</i></p> <p>I'm ashamed to post this as an option, but it's good to see what you can do with the array of strings available to <code>caller</code>. Let's just hope <code>caller</code> doesn't change the formatting.</p> <p>Using 2.3.15 from the console, the callstack to my model's after_find() is:</p> <pre> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/callbacks.rb:347:in `send' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/callbacks.rb:347:in `callback' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:1687:in `send' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:1687:in `instantiate' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:665:in `find_by_sql' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:665:in `collect!' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:665:in `find_by_sql' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:1582:in `find_every' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:1539:in `find_initial' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.15/lib/active_record/base.rb:696:in `exists?' (irb):2:in `irb_binding' /usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding' /usr/lib/ruby/1.8/irb/workspace.rb:52 </pre> <h3>C) Make the Model Eager-Load</h3> <p>Some people might want to do this, an the 2.3.15 ActiveRecord README hints at it:</p> <pre><code>after_find :eager_load, 'self.class.announce(#{id})' </code></pre> <p>I don't want to put any more burden on the DB than I must. The point of calling <code>exists?</code> in many cases is to do a simple single-column lookup. Performing eager loading of a whole model and potentially its associations just to do an <code>exists?</code> call is going to cause a performance hit.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload