<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.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>Inspire Creativity: Rails: Hack belongs_to to allow :select</title>
    <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Rails: Hack belongs_to to allow :select</title>
      <description>&lt;p&gt;
For some reason most rails associations allow :select as a parameter to limit which rows are loaded, but belongs_to doesn't.
&lt;/p&gt;&lt;p&gt;
Here's my (only rudimentarily tested) monkey-patch to add it:
&lt;/p&gt;&lt;pre&gt;module ActiveRecord::Associations::ClassMethods
  def create_belongs_to_reflection(association_id, options)
    $stderr.puts "MAKING REFLECTION"
    options.assert_valid_keys(
      :select, #added
      :class_name, :foreign_key, :foreign_type, :remote, :conditions, :order, :include, :dependent, 
      :counter_cache, :extend, :polymorphic
    )
    
    reflection = create_reflection(:belongs_to, association_id, options, self)

    if options[:polymorphic]
      reflection.options[:foreign_type] ||= reflection.class_name.underscore + "_type"
    end

    reflection
  end
end
module ActiveRecord
  module Associations
    class BelongsToAssociation  @reflection.options[:select] || '*', #added
          :conditions =&amp;gt; conditions,
          :include    =&amp;gt; @reflection.options[:include]
        )
      end
    end
  end
end
&lt;/pre&gt;
&lt;!-- technorati tags start --&gt;&lt;p style="text-align:right;font-size:10px;"&gt;Technorati Tags: &lt;a href="http://www.technorati.com/tag/monkey patching" rel="tag"&gt;monkey patching&lt;/a&gt;, &lt;a href="http://www.technorati.com/tag/ruby on rails" rel="tag"&gt;ruby on rails&lt;/a&gt;&lt;/p&gt;&lt;!-- technorati tags end --&gt;</description>
      <pubDate>Tue, 17 Apr 2007 17:57:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ea05a15f-42e2-4548-8db0-bdd322cb6f38</guid>
      <author>kevin</author>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select</link>
      <category>Coding</category>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Martin</title>
      <description>hey kevin i heard you created The Ap site

i was wondering if you could help me with the scritp for the chatterbox you have there

my email is &lt;a href="mailto:morbid.maney@gmail.com" rel="nofollow"&gt;morbid.maney@gmail.com&lt;/a&gt;</description>
      <pubDate>Wed, 14 May 2008 06:04:53 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:105f170c-c0a2-49ba-bc12-fd07e5f04957</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-79996</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by andrew</title>
      <description>Looks nice and works thanks!</description>
      <pubDate>Mon, 17 Mar 2008 06:53:16 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:62657826-20b6-4d43-894d-dae340675e21</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-66492</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Very Kewel Site </title>
      <description>I can see why a lot of people would really enjoy your links here.</description>
      <pubDate>Tue, 28 Aug 2007 20:54:24 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e324ab1f-170e-4588-a9f1-dbe43242d8d6</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-45565</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Edwin</title>
      <description>Really good work, I've just tried and I like it very much. Thanks!</description>
      <pubDate>Fri, 13 Jul 2007 03:24:12 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b71bb712-8777-4753-8324-401653c71571</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-43267</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Arnod</title>
      <description>Kevin, may I ask do you work on something else for the moment? Coz I can't find anything new at this hub...</description>
      <pubDate>Wed, 04 Jul 2007 04:40:41 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:1e9d89b1-a2e9-4d46-92a3-e4a5dae5fff0</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-42633</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Led</title>
      <description>Looks nice and works ;) thanks!</description>
      <pubDate>Wed, 23 May 2007 08:14:20 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b88d2788-38f2-4a9b-af30-629a202138d4</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-35239</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Kevin Watt</title>
      <description>I am also Kevin Watt. That's all</description>
      <pubDate>Wed, 23 May 2007 02:29:05 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:5bcdb6df-0a78-4124-abcb-b1edb2fd26cd</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-35233</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Jimy T.</title>
      <description>Wow thanks! I’m a regular reader and enjoy your work. Keep it up!</description>
      <pubDate>Tue, 08 May 2007 07:00:12 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4b8906ea-3f97-4477-b2bc-894de2190d14</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-30007</link>
    </item>
    <item>
      <title>"Rails: Hack belongs_to to allow :select" by Bill</title>
      <description>Very cool feature. Great that this service works on any site not just blogs.</description>
      <pubDate>Fri, 04 May 2007 15:56:14 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:60fd7c37-5705-4f10-a5e0-c0d871ac1916</guid>
      <link>http://kevinwatt.org/articles/2007/04/17/rails-hack-belongs_to-to-allow-select#comment-28697</link>
    </item>
  </channel>
</rss>
