<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Elevated Rails RSS Feed</title>
    <link>http://www.elevatedrails.com/rss/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Elevated Rails, A Chicago based software development consultancy.</description>


        <item>
          <title>Painless monitoring with Scout</title>
          <description>&lt;p&gt;
I write about monitoring from time to time. We host about 15 applications for our clients, so monitoring our servers is a high priority for us. We've experimented with a large number of tools including &lt;a href=&quot;http://www.tildeslash.com/monit/&quot;&gt;monit&lt;/a&gt; and &lt;a href=&quot;http://god.rubyforge.org/&quot;&gt;god&lt;/a&gt;. Both work great as process monitors, but they aren't built for some of our most important tasks. Enter &lt;a href=&quot;http://www.scoutapp.com&quot;&gt;Scout&lt;/a&gt;. In less then a week I've been able to port all of our ad-hoc monitors to Scout. See inside for more information.
&lt;/p&gt;

 &lt;p&gt;
 What makes Scout great is its configuration and plugin architecture. When a host runs it's monitors, it talks to Scout to download an executiong plan. This plan includes a list of plugins to execute. If a plugin has been added, the client will automatically download the required code. This makes it trivial to change monitors via the web and have your clients update automatically.
&lt;/p&gt;
&lt;p&gt;
A Scout plugin is just a simple Ruby class that either returns data, sends and alert or raises an error. It provides a very simple architecture for adding new plugins. This means that unlike with monit, you can create monitors that are tailored to your environment.
&lt;/p&gt;
&lt;p&gt;
  Let's look at an example. Weare heavy users of &lt;a href=&quot;http://github.com/advany/starling/tree/master&quot;&gt;Starling&lt;/a&gt;. We want to be able to monitor certain queues to make sure that our processors are running. Doing this under monit was a hack. I wrote a ruby program to read the queues and touch a watchdog file when the queue depth is below a threshold. I configured monit to raise an error when a file's timestamp was older than a set interval. This worked in practice, but it was complicated to set up and required modifying multiple pieces any time a change was required.
&lt;/p&gt;
&lt;p&gt;
 I was able to replace my cobbled together monitor in just a few minutes with Scout. Here is the full code to my new plugin:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;MissingLibrary&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;StandardError&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;StarlingMonitor&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Scout&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Plugin&lt;/span&gt;


  &lt;span class=&quot;ident&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:connection&lt;/span&gt;


  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;setup_starling&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;begin&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;starling&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;LoadError&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;begin&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;rubygems&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;starling&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;LoadError&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;raise&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;MissingLibrary&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Starling&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;expr&quot;&gt;#{option(:host)}&lt;/span&gt;:&lt;span class=&quot;expr&quot;&gt;#{option(:port)}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;



  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;build_report&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;begin&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;setup_starling&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;connection&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;sizeof&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:all&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;queue_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;item_count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;check_queue&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;queue_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;item_count&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;should_check_queue?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;queue_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;attribute&quot;&gt;@report&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;rescue&lt;/span&gt;  &lt;span class=&quot;constant&quot;&gt;MissingLibrary&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Could not load all required libraries&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;,&lt;/span&gt;
            &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;I failed to load the starling library. Please make sure it is installed.&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Got unexpected error: &lt;span class=&quot;expr&quot;&gt;#{e}&lt;/span&gt; &lt;span class=&quot;expr&quot;&gt;#{e.class}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;should_check_queue?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:queue_re&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;nil?&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;regex&quot;&gt;&lt;span class=&quot;expr&quot;&gt;#{option(:queue_re)}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=~&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;check_queue&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;q_depth&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;depth&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt;&lt;span class=&quot;number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_i&lt;/span&gt;
    &lt;span class=&quot;attribute&quot;&gt;@report&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{}&lt;/span&gt;
    &lt;span class=&quot;attribute&quot;&gt;@report&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;q_depth&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;q_depth&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;option&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:max_depth&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_i&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;alert&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Max Queue Depth for &lt;span class=&quot;expr&quot;&gt;#{name}&lt;/span&gt; exceeded&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;,&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;expr&quot;&gt;#{q_depth}&lt;/span&gt; items is more than the max allowed &lt;span class=&quot;expr&quot;&gt;#{option(:max_depth)}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  There's a bit of code, but it is pretty simple. Scout starts our plugin by calling the &lt;code&gt;build_report&lt;/code&gt; method. In &lt;code&gt;build_report&lt;/code&gt;, I do a little setup to make sure  can load all of the required libraries. After requiring in Starling, I make a connection. The plugin uses the &lt;code&gt;option&lt;/code&gt; method to pull configuration information from the environment. Scout provides a simple configuration interface that allows plugins to be configured via the web.
&lt;/p&gt;
&lt;p&gt;
 After loading up the environment, I walk the list of matching queues and send any necessary alerts. After looking at all queues, I return a report that maps the name of each queue to its depth. Along with providing an alert infrastructure, Scout also stores your reported data and can plot it on a graph. For example, here is a graph of the depth of our starling queues on one server:
&lt;/p&gt;
&lt;img src=&quot;/queue_depth.png&quot; /&gt;
&lt;p&gt;
 This simple interface to reporting and alerting is incredibly powerful. What makes it even more powerful is the ease at which you can install plugins. If you want to install my starling plugin, you can simply enter the URL to the Ruby file from github (http://github.com/mmangino/er-scout-plugins/tree/master%2Fstarling_monitor%2Fstarling_monitor.rb?raw=true)
&lt;/p&gt;
&lt;p&gt;
  As I said, I've moved all of my ad-hoc monitoring to Scout. I had to create a few plugins to make that work. They're available on Github at &lt;a href=&quot;http://github.com/mmangino/er-scout-plugins/tree/master&quot;&gt;http://github.com/mmangino/er-scout-plugins/tree/master&lt;/a&gt;. Feel free to fork them and submit enhancements!
&lt;/p&gt;
&lt;p&gt;
 So far, using Scout has been a real joy. That's something I never thought I would say about a monitoring tool.
&lt;/p&gt;</description>
          <pubDate>Wed, 06 Aug 2008 10:43:34 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/08/06/painless-monitoring-with-scout/</guid>
          <link>http://www.elevatedrails.com/articles/2008/08/06/painless-monitoring-with-scout/</link>
        </item>

        <item>
          <title>Memcached sessions and Facebook</title>
          <description>&lt;p&gt;
  Memcached makes an excellent session store for Facebook applications. Unfortunately, it doesn't work out of the box with &lt;a href=&quot;http://github.com/mmangino/facebooker/tree/master&quot;&gt;Facebooker&lt;/a&gt;. See how to fix that inside.&lt;/p&gt;
 &lt;p&gt;
By default, the Memcached session store doesn't allow session keys with dashes in them. Since Facebook session identifiers include a dash, this causes an immediate error. Luckily, there is a simple fix. Add the following code as either an initializer or to your environment.rb file to get your Facebooker sessions running with memcached.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;comment&quot;&gt;# add - as an okay key&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;CGI&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;Session&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;MemCacheStore&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;check_id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;comment&quot;&gt;#:nodoc:#&lt;/span&gt;
        &lt;span class=&quot;punct&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;regex&quot;&gt;[^0-9a-zA-Z-]+&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=~&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;true&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
 Don't you love simple solutions to annoying problems?
&lt;/p&gt;</description>
          <pubDate>Fri, 25 Jul 2008 09:41:24 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/07/25/memcached-sessions-and-facebook/</guid>
          <link>http://www.elevatedrails.com/articles/2008/07/25/memcached-sessions-and-facebook/</link>
        </item>

        <item>
          <title>We're looking for a few people to test a new product</title>
          <description>&lt;p&gt;
  We've been taking all of our payments from clients via ACH or electronic check for the last few months. We've really enjoyed the traceability and dependability that these payments give us. Along the way, we built a very simple payment solution that our clients use to pay our invoices. We're looking for 3 or 4 other people who are interested in trying it out. Our solution will be available within the next month and will be free for our testers. We can help you get the necessary merchant accounts if you need them. 
&lt;/p&gt;
&lt;p&gt;
Are you interested? Send me an email at &lt;a href=&quot;mailto:mmangino@elevatedrails.com&quot;&gt;mmangino@elevatedrails.com&lt;/a&gt; or give me a call at 773.259.0145 if you are interested.
&lt;/p&gt; </description>
          <pubDate>Fri, 18 Jul 2008 06:56:57 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/07/18/were-looking-for-a-few-people-to-test-a-new-product/</guid>
          <link>http://www.elevatedrails.com/articles/2008/07/18/were-looking-for-a-few-people-to-test-a-new-product/</link>
        </item>

        <item>
          <title>Major Facebooker Updates</title>
          <description>&lt;p&gt;
  About a month ago I moved Facebooker development to &lt;a href=&quot;http://github.com/mmangino/facebooker/tree/master&quot;&gt;GitHub&lt;/a&gt;. What a great decision that was. I've applied a flurry of patches to fix some outstanding issues and add some additional support to the platform. If you haven't done any Facebook development, Facebooker is a Ruby library for creating Facebook Applications. It is the library that I used in &lt;a href=&quot;http://www.pragprog.com/titles/mmfacer&quot;&gt;Developing Facebook Platform Applications with Rails&lt;/a&gt;. Details on the recent changes inside.
&lt;/p&gt; &lt;p&gt; In the last few days, we've added a lot of new features. The biggest of which is support for Bebo and other social platforms. &lt;a href=&quot;http://workingwithrails.com/person/5257-david-clements&quot;&gt;David Clements&lt;/a&gt; forked the repository and made massive internal changes to support multiple backends. With his change I was able to get a client's application running on Bebo in just a few minutes. Great work David! &lt;a href=&quot;http://github.com/redinger&quot;&gt;Chris Redinger&lt;/a&gt; submitted several changes including support for forgery protection in Facebook forms. Brian Lee added support for friend lists as well. 
&lt;/p&gt;
&lt;p&gt;
On top of those changes, I added support for the new action templates to both Facebooker and the Publisher. The Facebooker Publisher is an ActionMailer like interface to Facebook messaging.
Check out the documentation (&lt;a href=&quot;http://facebooker.rubyforge.org/&quot;&gt;http://facebooker.rubyforge.org/&lt;/a&gt; and &lt;a href=&quot;http://facebooker.rubyforge.org/classes/Facebooker/Rails/Publisher.html&quot;&gt;http://facebooker.rubyforge.org/classes/Facebooker/Rails/Publisher.html&lt;/a&gt; in particular)
&lt;/p&gt;
&lt;p&gt;
If you've already created a publisher, you will need to re-run the script/generate command to create a new migration. By default, Facebooker will now keep track of your template ids. To send a message using the new API, you'll need to create two methods. The first returns the templates. For example:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;MyPublisher&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;Facebooker&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Publisher&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;publish_action_template&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;one_line_story_template&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;{*actor*} did stuff with {*friend*}&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;one_line_story_template&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;{*actor*} did stuff&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;short_story_template&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;{*actor*} has a title {*friend*}&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:partial=&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;short_body&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;short_story_template&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;{*actor*} has a title&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:partial=&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;short_body&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;full_story_template&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;{*actor*} has a title {*friend*}&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;render&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:partial=&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;full_body&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
The name of the method needs to end in _template.
&lt;/p&gt;
&lt;p&gt;
To register this template, you can  call
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;constant&quot;&gt;MyPublisher&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;register_publish_action&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
or, to register all templates in a publisher, you can call
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;constant&quot;&gt;MyPublisher&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;register_all_templates&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
Facebooker will store the template id and the template name in the facebook_templates table.
&lt;/p&gt;

&lt;p&gt;
To send a message from a stored template, you'll need to create another method that registers the data:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;

 &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;publish_action&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
   &lt;span class=&quot;ident&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;f&lt;/span&gt;
   &lt;span class=&quot;ident&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:friend=&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;Mike&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
 &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
Then, you can call
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;constant&quot;&gt;MyPublisher&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;deliver_publish_action&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
Facebooker will look up the template id automatically.
&lt;/p&gt;
&lt;p&gt;
  This will all be covered in a forthcoming Beta of &lt;a href=&quot;http://www.pragprog.com/titles/mmfacer&quot;&gt;my book&lt;/a&gt;.
&lt;/p&gt;

</description>
          <pubDate>Wed, 16 Jul 2008 08:42:13 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/07/16/major-facebooker-updates/</guid>
          <link>http://www.elevatedrails.com/articles/2008/07/16/major-facebooker-updates/</link>
        </item>

        <item>
          <title>Our schedule is opening</title>
          <description>&lt;p&gt;
 We've been working pretty hard around Elevated Rails. We've launched a few new client projects this year and worked on some pretty massive applications. We've &lt;a href=&quot;http://www.pragprog.com/titles/mmfacer&quot;&gt;written a book&lt;/a&gt; and &lt;a href=&quot;http://www.elevatedrails.com/presentations/facebook&quot;&gt;talked at RailsConf&lt;/a&gt;. We're even getting ready to launch a &lt;a href=&quot;http://tunnlr.com&quot;&gt;simple application&lt;/a&gt; to make Facebook development easier. We're finally starting to see some openings appear in our schedule. What does that mean? It's time to fill them!
&lt;/p&gt; &lt;p&gt;
 If you are looking for a top notch development team to work on your application, let us know. We've built sites that handle serious load. We've built applications for Facebook and applications that integrate with telephone systems. We're test driven and ready to build your next application. 
&lt;/p&gt;
&lt;p&gt;
 Give me (Mike) a call at 773.259.0145 to discuss your project. We'll talk about your goals and our abilities and see if we're a good match. If we're not, we'll try to recommend somebody who can do a great job for you.
&lt;/p&gt;</description>
          <pubDate>Tue, 08 Jul 2008 12:28:06 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/07/08/our-schedule-is-opening/</guid>
          <link>http://www.elevatedrails.com/articles/2008/07/08/our-schedule-is-opening/</link>
        </item>

        <item>
          <title>Facebook Podcast</title>
          <description>&lt;p&gt;
It's been a hectic week around here. After moving to Philadelphia on Sunday, I participated in a Podcast with Susannah Pflazer, my editor for &lt;a href=&quot;http://www.pragprog.com/titles/mmfacer&quot;&gt;Developing Facebook Platform Applications with Rails&lt;/a&gt;. You can listen to the interview at
&lt;a href=&quot;http://pragprog.com/podcasts&quot;&gt;Pragmatic Bookshelf Podcasts&lt;/a&gt;. On top of that, I'm also hard at work updating the facebooker library for the upcoming changes. Hopefully things will slow down shortly to give Jen and I some time to enjoy our new city!&lt;/p&gt; </description>
          <pubDate>Thu, 19 Jun 2008 06:25:46 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/06/19/facebook-podcast/</guid>
          <link>http://www.elevatedrails.com/articles/2008/06/19/facebook-podcast/</link>
        </item>

        <item>
          <title>I'm speaking at RailsConf</title>
          <description>&lt;p&gt;My talk about Facebook Development is at 1:50 on Friday.&lt;/p&gt;


	&lt;p&gt;You can &lt;a href=&quot;http://www.elevatedrails.com/presentations/facebook.zip&quot;&gt;download the slides.&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;You can also &lt;a href=&quot;http://www.elevatedrails.com/presentations/facebook&quot;&gt;view them online&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Stop by and say Hi!&lt;/p&gt; </description>
          <pubDate>Fri, 30 May 2008 10:33:41 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/05/30/im-speaking-at-railsconf/</guid>
          <link>http://www.elevatedrails.com/articles/2008/05/30/im-speaking-at-railsconf/</link>
        </item>

        <item>
          <title>My Recipe Featured in a Video</title>
          <description>&lt;p&gt;
I was really excited to have several recipes included in &lt;a href=&quot;http://pragprog.com/titles/fr_arr/advanced-rails-recipes&quot; &gt;Advanced Rails Recipes&lt;/a&gt;.

It's even cooler to have one of them shown in &lt;a href=&quot;http://videos.pragprog.com/book-related/fr_arr.mov&quot;&gt;the video&lt;/a&gt;. Thanks to Mike Clark for pulling together a really great reference for Rails developers.
&lt;/p&gt;
&lt;p&gt;
Sorry about the lag between posts. A vacation to Hawaii interfered with my schedule :)
&lt;/p&gt; </description>
          <pubDate>Tue, 13 May 2008 16:15:46 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/05/13/my-recipe-featured-in-a-video/</guid>
          <link>http://www.elevatedrails.com/articles/2008/05/13/my-recipe-featured-in-a-video/</link>
        </item>

        <item>
          <title>Building a plugin (form_for and overriding methods continued)</title>
          <description>&lt;p&gt;
In my last post, we looked at why Rails doesn't use getter methods in form_for. We also came up with a relatively ugly fix. In this post, we'll clean that code up and turn it into a plugin.
&lt;/p&gt; &lt;p&gt;
 Previously, we figured out that we could override &lt;code&gt;respond_to?&lt;/code&gt; to make form_for use getter methods on our Rails models. We ended up with the following code to do this for our &lt;code&gt;pending_email&lt;/code&gt; method.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;pending_email_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
 We will make our code more modular in a couple of steps. First, let's change our &lt;code&gt;respond_to?&lt;/code&gt; method to deal with an array of column names instead of a hard coded value. We could easily do something like:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@use_getters&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
 That code would work, but there is a problem. Notice that &lt;code&gt;@use_getters&lt;/code&gt; is an instance variable. We probably don't want to create a variable to hold the list of columns to use getters for on every model object. Instead, we could store that variable in a class variable and create accessors for it. We probably won't ever remove an item from this list of attributes, let's create two methods &lt;code&gt;add_method_to_use_getter&lt;/code&gt; and &lt;code&gt;methods_to_use_getters&lt;/code&gt;. There's nothing tricky in these methods as you can see:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt; 
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;self.add_method_to_use_getter&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;+&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;self.methods_to_use_getters&lt;/span&gt;
    &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  In our code, we first make sure that our array exists. If it does, we add the name of the method to hide to our array. In the next method, we return either the array of method names or an empty array. Next, we can modify our &lt;code&gt;respond_to?&lt;/code&gt; method.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;methods_to_use_getters&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
With that done, we have just a couple of remaining steps to make this look nice. First, let's create make it so that the following code will work:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;use_getters_for&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:pending_email&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:other_attribute&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  Doing that is easier than you might think. All we need is a &lt;code&gt;use_getters_for&lt;/code&gt; method that takes a variable number of arguments. That is simply:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;self.use_getters_for&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;add_method_to_use_getter&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  Next, let's turn this into a plugin so that we don't have to constantly define all of those methods on our classes. To create a plugin, we'll start by running &lt;code&gt;script/generate plugin uses_getters_for_forms
&lt;/code&gt; to create a skeleton plugin. Next, we'll create a couple of modules inside the generated &lt;code&gt;lib/uses_getters_for_forms.rb&lt;/code&gt;. We will need to create two modules. One for instance methods and one for class methods. (Including a module only adds instance methods. This is explained brilliantly in &lt;a href=&quot;http://mtnwestrubyconf2008.confreaks.com/11farley.html&quot;&gt;Patrick Farley's Mountain West Ruby Conf talk&lt;/a&gt;.) To avoid naming conflicts, we will put both of these modules into the &lt;code&gt;UsesGettersForForms&lt;/code&gt; module.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;comment&quot;&gt;# UsesGettersForForms&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;UsesGettersForForms&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;ClassMethods&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;use_getters_for&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;add_method_to_use_getter&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;add_method_to_use_getter&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;+&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;methods_to_use_getters&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;InstanceMethods&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;methods_to_use_getters&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

&lt;/pre&gt;

&lt;p&gt;
 With that done, we just need to make that method available to all of our models. We customarily do that by including it in ActiveRecord::Base. We can do that for our class methods, but not for our instance methods. As Patrick Farley explains, Our included &lt;code&gt;respond_to?&lt;/code&gt; method won't be called until after the Rails method is called. Tow work around this, we'll dynamicly include our &lt;code&gt;respond_to?&lt;/code&gt; method only when we need to. Here is the code to put into &lt;code&gt;init.rb&lt;/code&gt; to do this.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;comment&quot;&gt;# Include hook code here&lt;/span&gt;
&lt;span class=&quot;ident&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;uses_getters_for_forms&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;'&lt;/span&gt;
&lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:extend&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;UsesGettersForForms&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;ClassMethods&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  Finally, we need to modify our &lt;code&gt;use_getters_for&lt;/code&gt; method to add the &lt;code&gt;respond_to?&lt;/code&gt; method the first time this is called.
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;comment&quot;&gt;# UsesGettersForForms&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;UsesGettersForForms&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;ClassMethods&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;use_getters_for&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@ugff_loaded&lt;/span&gt;
        &lt;span class=&quot;ident&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;UsesGettersForForms&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;InstanceMethods&lt;/span&gt;
        &lt;span class=&quot;attribute&quot;&gt;@ugff_loaded&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;true&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;{|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;add_method_to_use_getter&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;add_method_to_use_getter&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;+&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;methods_to_use_getters&lt;/span&gt;
      &lt;span class=&quot;attribute&quot;&gt;@methods_to_use_getters&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;[]&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;module &lt;/span&gt;&lt;span class=&quot;module&quot;&gt;InstanceMethods&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;methods_to_use_getters&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
There's a lot of code there, and a lot of it is somewhat tricky. Don't just copy and paste this code. Understand &lt;a href=&quot;http://blog.jayfields.com/2008/04/understanding-why.html&quot;&gt;why&lt;/a&gt; it works. Watch the video by Patrick Farley and play with the code. Next, we'll look at why this breaks for STI models and how we can fix it.
&lt;/p&gt;
&lt;/p&gt;
</description>
          <pubDate>Mon, 28 Apr 2008 19:42:39 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/04/28/building-a-plugin-form_for-and-overriding-methods-continued/</guid>
          <link>http://www.elevatedrails.com/articles/2008/04/28/building-a-plugin-form_for-and-overriding-methods-continued/</link>
        </item>

        <item>
          <title>form_for and overriding methods</title>
          <description>&lt;p&gt;
 From time to time, we override ActiveRecord attribute methods to add some additional behavior. Yesterday, I was working on some code to require a user to approve a new email address before the change was made and ran into some problems using my new methods in forms. I'll show you how to fix this inside
&lt;/p&gt; &lt;p&gt;
  First, let's take a look at the code in question. I have a &lt;code&gt;User&lt;/code&gt; model with an &lt;code&gt;email&lt;/code&gt; attribute. When a user wants to change their email address, I want to send an email to their new address and delay the actual change until they click a confirmation link.
&lt;/p&gt;
&lt;p&gt;
 To implement this, I added a &lt;code&gt;pending_email&lt;/code&gt; attribute on the user mode. I then use this method in all of my forms for users. By default, the &lt;code&gt;pending_email&lt;/code&gt; attribute is blank. When a user views their profile edit form, I want them to see their existing email address. It should be easy to make the &lt;code&gt;pending_email&lt;/code&gt; attribute return either itself if it is set, or the &lt;code&gt;email&lt;/code&gt; attribute if it is blank. That method looks like:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;pending_email&lt;/span&gt;
    &lt;span class=&quot;ident&quot;&gt;read_attribute&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;symbol&quot;&gt;:pending_email&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;email&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  That does exactly what we want. Calling &lt;code&gt;pending_email&lt;/code&gt; gives us the email address by default. Unfortunately, things break down when we try to use this method in a form. When we do something like:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
  &lt;span class=&quot;punct&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;form_for&lt;/span&gt; &lt;span class=&quot;attribute&quot;&gt;@user&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;
    &amp;lt;%=f.text_field :pending_email %&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;punct&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;&lt;span class=&quot;normal&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
 Our pending email never displays the existing email address. Once again, to understand what was happening, I had to dig in to the Rails source. I started by looking at the &lt;code&gt;text_field&lt;/cf&gt; method in form_helper.rb. That pointed me to the &lt;code&gt;InstanceTag&lt;/code&gt; class. That finally led me to the following code:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;

        &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;value_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;method_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;nil?&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method_name&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;?&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method_name&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;)&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;ident&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;method_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
        &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  That code looks at the object in our form, and sees whether it has a &lt;code&gt;pending_email_before_type_cast&lt;/code&gt; method. If it does, it calls it to get the value. If it doesn't, it calls &lt;code&gt;pending_email&lt;/code&gt;. By default, ActiveRecord creates the _before_type_cast methods so that code can access the textual value of a field before it is turned into a ruby object. For example, calling &lt;code&gt;created_at_before_type_cast&lt;/code&gt; will give you the textual representation of a date inside the database. There are good reasons for this behavior, but they don't really matter to us. All we need to know is that our pending email method doesn't every get called.
&lt;/p&gt;
&lt;p&gt;
 So how do we fix this? I'll show you a hack to make it work today, and then tomorrow, or sometime in the next few days, I'll show you how to turn this into a plugin. We're going to exploit the trick that the &lt;code&gt;value_before_type_cast&lt;/code&gt; method checks to see if our object responds to a method before it calles the _before_type_cast method. We can easily override respond_to? to make it lie about the existence of our method. Here's the code that I used:
&lt;/p&gt;
&lt;pre class=&quot;code_ruby&quot;&gt;
  &lt;span class=&quot;comment&quot;&gt;# fake out the helpers to use the method and not the raw attribute&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;respond_to?&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;(*&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;to_s&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;string&quot;&gt;pending_email_before_type_cast&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;&amp;quot;&lt;/span&gt;
      &lt;span class=&quot;constant&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;keyword&quot;&gt;super&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;
  That's all it takes! Our code checks to see if the caller is asking about our &lt;code&gt;pending_email&lt;/code&gt; method. If they are, it lies and says the _before_type_cast method doesn't exist. For anything else, we fall back to the default behavior. Now, our users see their existing email address when they view their profile edit form.
&lt;/p&gt;
&lt;p&gt;You can see why we want to clean up this code a little. It's not obvious what that code is doing, and it's really easy to get wrong. (I spent 20 minutes debugging because I spelled type_cast wrong. I don't want to have to ever do that again)
&lt;/p&gt;
&lt;p&gt;
  Expect to see more frequent posting here. My book, &lt;a href=&quot;http://www.pragprog.com/titles/mmfacer&quot;&gt;Developing Facebook Platform Applications with Rails&lt;/a&gt; is almost done and I will have a little bit more time to write.
&lt;/p&gt;</description>
          <pubDate>Thu, 24 Apr 2008 08:49:43 GMT</pubDate>
          <guid>http://www.elevatedrails.com/articles/2008/04/24/form_for-and-overriding-methods/</guid>
          <link>http://www.elevatedrails.com/articles/2008/04/24/form_for-and-overriding-methods/</link>
        </item>


  </channel>
</rss>

