<?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>duck_punching &#187; activerecord</title>
	<atom:link href="http://duckpunching.com/topics/activerecord/feed" rel="self" type="application/rss+xml" />
	<link>http://duckpunching.com</link>
	<description>Ruby, Rails and tech. No ducks were harmed in the making of this blog.</description>
	<lastBuildDate>Thu, 24 Jun 2010 07:12:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Passing a block to ActiveRecord create</title>
		<link>http://duckpunching.com/passing-a-block-to-activerecord-create</link>
		<comments>http://duckpunching.com/passing-a-block-to-activerecord-create#comments</comments>
		<pubDate>Thu, 08 May 2008 02:57:20 +0000</pubDate>
		<dc:creator>Adam</dc:creator>
				<category><![CDATA[activerecord]]></category>
		<category><![CDATA[patches]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[create]]></category>

		<guid isPermaLink="false">http://duckpunching.com/?p=4</guid>
		<description><![CDATA[I submitted a patch for Rails, which has been committed, to allow you to pass a block to the AR create class method. It got a mention on the RailsEnvy podcast in the Living on the Edge section. It mind sound boring, but it allows you to pretty up the regular code such as creating [...]]]></description>
			<content:encoded><![CDATA[<p>I submitted a <a href="http://github.com/rails/rails/commit/dd120ede53eaf71dee76894998a81626b7a689fc" title="patch">patch</a> for Rails, which has been committed, to allow you to pass a block to the AR create class method. It got a mention on the <a href="http://www.railsenvy.com/2008/5/7/rails-envy-podcast-episode-030-05-07-2008" title="Rails Envy podcast">RailsEnvy</a> podcast in the <a href="http://blog.codefront.net/2008/05/04/living-on-the-edge-of-rails-19-change_table-for-migrations-and-more/" title="Living on the Edge">Living on the Edge</a> section.</p>
<p>It mind sound boring, but it allows you to pretty up the regular code such as creating a new record from the parameters hash to then save it.</p>
<p>Often you might</p>
<pre lang="ruby">
@person = Person.new(params[:person])
@person.set_status :alive
@person.group = "default"
@person.save</pre>
<p>now you can pretty it up with</p>
<pre lang="ruby">
@person = Person.create(params[:person]) do |p|
  p.set_status :alive
  p.group = "default"
end</pre>
<p>The inner workings are that it creates a new object using the hash and then passes the object into the block for further manipulation. After the block is executed the record is saved and returned.</p>
<p>It can be particularly useful if you have a few protected attributes that you need to set or some instance method you need to execute when you are creating a record. Admittedly the set_status method might best be run as a callback in the model, but you get the idea.</p>
<p>It also works for the array variant of create when you pass in an array of hashes. The code in the block is executed for each record.</p>
<p>So look for it in Rails 2.1 or use it now with Edge.</p>
<p><strong>Footnote</strong>: The <a href="http://rails.lighthouseapp.com/projects/8994/tickets/39-add-blocks-to-ar-create-and-update">original patch</a> was actually larger and included the update method as well. Though <a href="http://rails.lighthouseapp.com/projects/8994/tickets/39-add-blocks-to-ar-create-and-update#ticket-39-12">DHH was not in favour </a>of it, mentioning that the class update method itself may be of dubious value.</p>
]]></content:encoded>
			<wfw:commentRss>http://duckpunching.com/passing-a-block-to-activerecord-create/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

