<?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>Adam Loving&#039;s Blog &#187; ActiveRecord</title>
	<atom:link href="http://adamloving.com/tag/activerecord/feed" rel="self" type="application/rss+xml" />
	<link>http://adamloving.com</link>
	<description>Seattle social web developer and marketing hacker</description>
	<lastBuildDate>Tue, 03 Apr 2012 09:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Rails ActiveRecord Tips</title>
		<link>http://adamloving.com/internet-programming/rails-activerecord-tips</link>
		<comments>http://adamloving.com/internet-programming/rails-activerecord-tips#comments</comments>
		<pubDate>Wed, 24 Dec 2008 01:08:21 +0000</pubDate>
		<dc:creator>Adam Loving</dc:creator>
				<category><![CDATA[Projects, Programming, Technology]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://adamloving.com/?p=837</guid>
		<description><![CDATA[After using ActiveRecord for a while, some questions keep coming up when performing operations that are more complicated than the standard create, read, update, delete.]]></description>
			<content:encoded><![CDATA[<p></p><div class="zemanta-img" style="margin: 1em; float: right; display: block;">
<div>
<dl class="wp-caption" style="width: 212px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:Ruby_on_Rails_logo.jpg"><img title="Ruby on Rails" src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/9c/Ruby_on_Rails_logo.jpg/202px-Ruby_on_Rails_logo.jpg" alt="Ruby on Rails" width="202" height="240" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:Ruby_on_Rails_logo.jpg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>After using ActiveRecord for a while, some questions keep coming up when performing operations that are more complicated than the standard create, read, update, delete.</p>
<h3>1. Does ActiveRecord track if an attribute is has been modified? Does it track dirty state on an object or attribute basis?</h3>
<p>Yes, but not for attributes so far as I can tell. Rails provides these great helper methods (examples given for a person sample object).</p>
<pre>person.new_record?
person.changed?
person.name_changed?
person.name_was

person.name = 'bob'
person.changed        # =&gt; ['name']
person.changes        # =&gt; { 'name' =&gt; ['Bill', 'bob'] }</pre>
<p><a href="http://api.rubyonrails.com/classes/ActiveRecord/Dirty.html">Documentation on Rails ActiveRecord dirty state methods<br />
</a><br />
note that changed? does not capture if a collection has changed (nor seem to work for a collection)</p>
<h3>2. Does ActiveRecord automatically set the reference pointers on both sides of a has_many relationship?</h3>
<p>Surprisingly, no. For example</p>
<pre>p = Person.new
e = EmailAddress.new
e.person = p
p. email_addresses # =&gt; []</pre>
<p>even though EmailAddress belongs_to :person</p>
<p>the reverse is also true</p>
<pre>e2 = EmailAddress.new
p.email_addresses.push(e2)
e2.entity # =&gt; nil</pre>
<p>Also, this is the case even if p and e have been previously saved to the database.</p>
<h3>3. Are objects automatically persisted when you insert into a collection?</h3>
<p>Yes. From the <a href="http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html">documentation</a>: &#8220;Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object, except if the parent object (the owner of the collection) is not yet stored in the database.&#8221;</p>
<p>You can add an object to a collection without automatically saving it by using the my_collection.build method</p>
<h3>4. How does it handle database sessions, is there support for transactions?</h3>
<p>I don&#8217;t see any support for sessions (in the &#8220;Hibernate&#8221; sense, where you can make a number of object changes, then dump those to the database all at once). However, simple transactions are definitely supported.</p>
<pre>ActiveRecord::Base.transaction do
  david.withdrawal(100)
  mary.deposit(100)
end</pre>
]]></content:encoded>
			<wfw:commentRss>http://adamloving.com/internet-programming/rails-activerecord-tips/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

