<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sam Pierson&#039;s Blog &#187; ActiveRecord</title>
	<atom:link href="http://sampierson.com/blog/tag/activerecord/feed/" rel="self" type="application/rss+xml" />
	<link>http://sampierson.com/blog</link>
	<description>Augmentation of an Imperfect Memory.</description>
	<lastBuildDate>Wed, 21 Jul 2010 01:41:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to stop ActiveRecord putting attribute name in validation message</title>
		<link>http://sampierson.com/blog/software-development/how-to-stop-activerecord-putting-attribute-name-in-validation-message/</link>
		<comments>http://sampierson.com/blog/software-development/how-to-stop-activerecord-putting-attribute-name-in-validation-message/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:43:12 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://sampierson.com/blog/?p=591</guid>
		<description><![CDATA[Use http://github.com/gumayunov/custom-err-msg/tree/master
Put a ^ at the start of the :message =&#62; option:
Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
 in which case the attribute name is omitted.
]]></description>
			<content:encoded><![CDATA[<p>Use http://github.com/gumayunov/custom-err-msg/tree/master</p>
<p>Put a ^ at the start of the :message =&gt; option:</p>
<pre style="font: normal normal normal 115%/normal Monaco, 'Courier New', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', monospace; line-height: 1.4em; font-family: 'Bitstream Vera Sans Mono', Courier, monospace; font-size: 11px; color: #444444; padding: 0px; margin: 0px;">Non-base messages are prefixed with the attribute name as usual UNLESS they begin with '^'
 in which case the attribute name is omitted.</pre>
]]></content:encoded>
			<wfw:commentRss>http://sampierson.com/blog/software-development/how-to-stop-activerecord-putting-attribute-name-in-validation-message/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Switching an ActiveRecord instance between STI classes</title>
		<link>http://sampierson.com/blog/software-development/switching-an-activerecord-instance-between-sti-classes/</link>
		<comments>http://sampierson.com/blog/software-development/switching-an-activerecord-instance-between-sti-classes/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 07:11:16 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://sampierson.com/blog/?p=582</guid>
		<description><![CDATA[Sometimes you wish to convert one STI class to another, e.g. promote User to Administrator.
ActiveRecord::Base#becomes(Klass) returns an instance of the current record converted to the specified Klass, e.g.:

&#160;
user = User.find_by_login&#40;&#34;sam&#34;&#41;
user.becomes&#40;Administrator&#41;

Note however that this does not change the type attribute of the instance, so you still have an additional step to do if you want this [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you wish to convert one STI class to another, e.g. promote User to Administrator.</p>
<p>ActiveRecord::Base#becomes(Klass) returns an instance of the current record converted to the specified Klass, e.g.:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">&nbsp;
user = User.<span style="color:#9900CC;">find_by_login</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;sam&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
user.<span style="color:#9900CC;">becomes</span><span style="color:#006600; font-weight:bold;">&#40;</span>Administrator<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Note however that this does not change the type attribute of the instance, so you still have an additional step to do if you want this conversion to be permanent.</p>
]]></content:encoded>
			<wfw:commentRss>http://sampierson.com/blog/software-development/switching-an-activerecord-instance-between-sti-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord performance: validation and associations</title>
		<link>http://sampierson.com/blog/software-development/rails-performance-and-validation/</link>
		<comments>http://sampierson.com/blog/software-development/rails-performance-and-validation/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 07:07:03 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://sampierson.com/blog/?p=475</guid>
		<description><![CDATA[If you have two models associated in some way, it is usual to add a validates_presence_of validator to ensure that the models are connected before saving. However, be aware that there are two ways of doing this with different performance ramifications:

class BlogEntry &#60; ActiveRecord::Base
  belongs_to :blog
  validate_presence_of :blog
end

During validation, Validates_presence_of :blog causes ActiveRecord [...]]]></description>
			<content:encoded><![CDATA[<p>If you have two models associated in some way, it is usual to add a validates_presence_of validator to ensure that the models are connected before saving. However, be aware that there are two ways of doing this with different performance ramifications:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BlogEntry <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:blog</span>
  validate_presence_of <span style="color:#ff3333; font-weight:bold;">:blog</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>During validation, <em>Validates_presence_of :blog</em> causes ActiveRecord to call <em>self#blog</em> to see if it is nil. As this is the name of the association it will cause AR to retrieve the associated record from the DB before ascertaining if it is nil.  E.g. with Rails 2.2.2:</p>
<pre>&gt;&gt; e = BlogEntry.find(1)
  BlogEntry Load (0.3ms)   SELECT * FROM "blog_entries" WHERE ("blog_entries"."id" = 1)
=&gt; #
&gt;&gt; e.valid?
  Blog Load (0.3ms)   SELECT * FROM "blogs" WHERE ("blogs"."id" = 1)
=&gt; true</pre>
<p>If you are confident of your database integrity, you can do <em>validates_presence_of :blog_id</em> instead.  This will save you an SQL query.  In a model that is saved frequently, this can be quite beneficial.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> BlogEntry <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:blog</span>
  validate_presence_of <span style="color:#ff3333; font-weight:bold;">:blog_id</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sampierson.com/blog/software-development/rails-performance-and-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActiveRecord eager-loading thru multiple associations</title>
		<link>http://sampierson.com/blog/software-development/activerecord-eager-loading-thru-multiple-associationsijust/</link>
		<comments>http://sampierson.com/blog/software-development/activerecord-eager-loading-thru-multiple-associationsijust/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 05:26:39 +0000</pubDate>
		<dc:creator>sam</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://sampierson.com/blog/?p=261</guid>
		<description><![CDATA[I just found out how to do something I have been wondering for a long time: :include syntax to eager-load through multiple associations. In the rdoc for class ActiveRecord::Associations::ClassMethods I discovered you can nest include statements in hashes as follows: If for example table_a has_many table_b and table_b has_many table_c and table_c has_many table_d:

Model.find :first, :include =&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I just found out how to do something I have been wondering for a long time: <em>:include</em> syntax to eager-load through multiple associations. In the rdoc for class <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html">ActiveRecord::Associations::ClassMethods</a> I discovered you can nest include statements in hashes as follows: If for example table_a has_many table_b and table_b has_many table_c and table_c has_many table_d:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Model.<span style="color:#9900CC;">find</span> <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:include</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:table_a</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:table_b</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:table_c</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:table_d</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://sampierson.com/blog/software-development/activerecord-eager-loading-thru-multiple-associationsijust/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
