<?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>Bugzappy&#039;s Blog &#187; send</title>
	<atom:link href="http://www.bugzappy.com/tag/send/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bugzappy.com</link>
	<description>on building internet apps</description>
	<lastBuildDate>Mon, 25 Jan 2010 14:24:31 +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>Setting the ID of new database records in Ruby-on-Rails</title>
		<link>http://www.bugzappy.com/2009/10/17/setting-the-id-of-new-database-records-in-ruby-on-rails/</link>
		<comments>http://www.bugzappy.com/2009/10/17/setting-the-id-of-new-database-records-in-ruby-on-rails/#comments</comments>
		<pubDate>Sun, 18 Oct 2009 04:30:02 +0000</pubDate>
		<dc:creator>bugzappy</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[find_or_create_by_id]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[seed]]></category>
		<category><![CDATA[send]]></category>

		<guid isPermaLink="false">http://www.bugzappy.com/?p=350</guid>
		<description><![CDATA[Switching over from Zend Framework to Ruby-on-Rails presents stumbling blocks. Rails wants to manage your database from A-to-Z. Fair enough.
But. When it comes to inserting data into it via rake db:migrate or rake db:seed (the latter being the correct way of managing your data separately from your schema), you may encounter outright frustration: Rails will [...]]]></description>
			<content:encoded><![CDATA[<p>Switching over from Zend Framework to Ruby-on-Rails presents stumbling blocks. Rails wants to manage your database from A-to-Z. Fair enough.</p>
<p>But. When it comes to inserting data into it via <strong>rake db:migrate</strong> or<strong> rake db:seed</strong> (the latter being the correct way of managing your data separately from your schema), you may encounter outright frustration: Rails will not let you set the id of the records!</p>
<p>For example, you write</p>
<blockquote><p>Singer.find_or_create_by_id(:id =&gt; 100, :name =&gt; &#8220;Donna Summer&#8221;, :rating =&gt; 2)</p></blockquote>
<p>but in your database, Dona&#8217;s id is just 1, or whatever the next available AUTO INCREMENT number was. I feel love &#8212; not.</p>
<p>There are at least two solutions, that hinge on getting around the fact the <strong>id </strong>is a <em>protected </em>attribute. First one:</p>
<blockquote><p>singer.find_or_create_by_id (:id =&gt; 100, :name =&gt; &#8220;Dona Summers&#8221;, :rating =&gt; 2) {|record| record.id = 100}</p></blockquote>
<p>or you can make use of the <strong>attributes=</strong> method of <strong>ActiveRecord::Base</strong> in the following way:</p>
<blockquote><p>Singer.attributes=({ :name =&gt; &#8216;Donna Summer&#8217;, :rating =&gt; 2}, false)</p></blockquote>
<p>The <em>false </em>passed as third parameter means &#8220;do not protect the protected attributes&#8221;. The RoR API documentation puts it this way:</p>
<blockquote><p><strong>attributes=</strong>(new_attributes, guard_protected_attributes = true)</p>
<p style="font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; color: #000000; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-size: small; background-position: initial initial;">Allows you to set <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002266">all</a> the <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348">attributes</a> at once by passing in a <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002357">hash</a> with keys matching the attribute names (which again matches the column names).</p>
<p style="font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; color: #000000; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-size: small; background-position: initial initial;">If <tt>guard_protected_attributes</tt> is true (the default), then sensitive <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348">attributes</a> can be protected from this form of mass-assignment by using the <tt><a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002280">attr_protected</a></tt> macro. Or you can alternatively specify which <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348">attributes</a> <strong>can</strong> be accessed with the <tt><a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002281">attr_accessible</a></tt> macro. Then <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002266">all</a> the <a style="color: #0000ff; text-decoration: none;" href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002348">attributes</a> not included in that won‘t be allowed to be mass-assigned.</p>
</blockquote>
<p style="font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; color: #000000; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-size: small; background-position: initial initial;">Look it up on <a href="http://api.rubyonrails.org/">http://api.rubyonrails.org/</a></p>
<p style="font-family: 'Bitstream Vera Sans', Verdana, Arial, Helvetica, sans-serif; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; color: #000000; margin-top: 0px; margin-right: 0px; margin-bottom: 0.5em; margin-left: 0px; font-size: small; background-position: initial initial;">
]]></content:encoded>
			<wfw:commentRss>http://www.bugzappy.com/2009/10/17/setting-the-id-of-new-database-records-in-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
