<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Tag docs</title>
    <link>http://www.robbyonrails.com/articles/tag/docs</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Observers Big and Small</title>
      <description>&lt;p&gt;My colleague, &lt;a href="http://blog.garyblessington.us"&gt;Gary&lt;/a&gt;, keeps a stack of Ruby and Rails books on his desk and was implementing an Observer into a client project. It appears that the Agile Web Development with Rails book is still encouraging people to do the following in order to load an Observer.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# app/models/flower_observer.rb
class FlowerObserver &amp;lt; ActiveRecord::Observer
  observe Flower

  def after_create(model)
    # model.do_something!
  end
end

# controller(s)
class FlowerController &amp;lt; ApplicationController
  observer :flower_observer
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;What is wrong with this approach?&lt;/p&gt;


	&lt;p&gt;Well, in order for your Observer to be used, the model(s) callbacks that it is observing need to be triggered through a controller. If you end up writing any scheduled rake tasks, your observer will not be called. In my opinion, the controller shouldn&amp;#8217;t know this much about the model. In fact, the model doesn&amp;#8217;t even really know about it&amp;#8217;s observer&amp;#8230; so why should a controller?&lt;/p&gt;


	&lt;p&gt;This was actually changed a long time ago (&lt;a href="http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go"&gt;I previously blogged about a different solution here&lt;/a&gt;) and the Rails docs for &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Observer.html"&gt;ActiveRecord::Observer&lt;/a&gt; are currently correct.&lt;/p&gt;


	&lt;h2&gt;Observers in the Environment&lt;/h2&gt;


	&lt;p&gt;If you open up a recent version of &lt;code&gt;config/environment.rb&lt;/code&gt;, you notice in the comments the following.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Take a moment to go ahead and specify which observer(s) you&amp;#8217;d like to load into your Rails environment.&lt;/p&gt;


&lt;pre&gt;config.active_record.observers = :flower_observer&lt;/pre&gt;

	&lt;p&gt;Then you can remove your observer calls in all your controllers, because that&amp;#8217;s not where you should be defining them.&lt;/p&gt;


	&lt;p&gt;Also, if you&amp;#8217;re not using Observers yet, I&amp;#8217;d really encourage you to consider reading up on them and giving them a try.&lt;/p&gt;
</description>
      <pubDate>Fri, 27 Apr 2007 13:15:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ebff46c4-4af0-4e95-b950-72023fff7d0a</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>activerecord</category>
      <category>development</category>
      <category>rails</category>
      <category>models</category>
      <category>observers</category>
      <category>docs</category>
    </item>
  </channel>
</rss>
