<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/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>Robby on Rails: Tag databases</title>
    <link>http://www.robbyonrails.com/articles/tag/databases</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Multiple Database Connections in Ruby on Rails</title>
      <description>&lt;p&gt;We have a client that already has some database replication going on in their deployment and needed to have most of their Ruby on Rails application pull from slave servers, but the few writes would go to the master, which would then end up in their slaves.&lt;/p&gt;


	&lt;p&gt;So, I was able to quickly extend ActiveRecord with just &lt;em&gt;two&lt;/em&gt; methods to achieve this. Anyhow, earlier today, someone in #caboose asked if there was any solutions to this and it prompted me to finally package this up into a quick and dirty Rails plugin.&lt;/p&gt;


	&lt;p&gt;Introducing&amp;#8230; &lt;strong&gt;Active Delegate&lt;/strong&gt;!&lt;/p&gt;


	&lt;p&gt;To install, do the following:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
cd vendor/plugins;
piston import http://svn.planetargon.org/rails/plugins/active_delegate
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Next, you&amp;#8217;ll need to create another database entry in your &lt;code&gt;database.yml&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
login: &amp;#38;login
  adapter: postgresql
  host: localhost
  port: 5432

development:
  database: rubyurl_development
  &amp;lt;&amp;lt;: *login

test:
  database: rubyurl_test
  &amp;lt;&amp;lt;: *login

production:
  database: rubyurl_servant
  &amp;lt;&amp;lt;: *login

# NOTICE THE NEXT ENTRY/KEY
master_database:
  database: rubyurl_master
  &amp;lt;&amp;lt;: *login
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;At this point, your Rails application won&amp;#8217;t talk to the &lt;code&gt;master_database&lt;/code&gt;, because nothing is being told to connect to it. So, the current solution with Active Delegate is to create an ActiveRecord model that will act as a connection handler.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/models/master_database.rb
  class MasterDatabase &amp;lt; ActiveRecord::Base
    handles_connection_for :master_database # &amp;lt;-- this matches the key from our database.yml
  end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, in the model(s) that we&amp;#8217;ll want to have talk to this database, we&amp;#8217;ll do add the following.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # app/models/animal.rb
  class Animal &amp;lt; ActiveRecord::Base
     delegates_connection_to :master_database, :on =&amp;gt; [:create, :save, :destroy]
  end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, when your application performs a &lt;code&gt;create&lt;/code&gt;, &lt;code&gt;save&lt;/code&gt;, or &lt;code&gt;destroy&lt;/code&gt;, it&amp;#8217;ll talk to the master database and your &lt;code&gt;find&lt;/code&gt; calls will retrieve data from your servant database.&lt;/p&gt;


	&lt;p&gt;It&amp;#8217;s late on a Friday afternoon and I felt compelled to toss this up for everyone. I think that this could be improved quite a bit, but it&amp;#8217;s working great for the original problem that needed to be solved.&lt;/p&gt;


	&lt;p&gt;If you have feedback and/or bugs, please &lt;a href="http://planetargon.lighthouseapp.com/projects/5187-open-source-projects/"&gt;send us tickets&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Fri, 05 Oct 2007 17:54:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:69a8625d-e24b-4f4e-aa58-d69a67784698</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>PostgreSQL</category>
      <category>PLANET ARGON</category>
      <category>databases</category>
      <category>replication</category>
      <category>activerecord</category>
      <category>plugins</category>
      <category>planetargon</category>
      <category>code</category>
    </item>
    <item>
      <title>Slides: Rails meets the Legacy World</title>
      <description>&lt;p&gt;Just got back to my hotel room after giving my talk, &lt;a href="http://www.planetargon.com/files/~robby/presentation_rails_meets_legacy_world.html"&gt;Rails meets the legacy world&lt;/a&gt; here at &lt;a href="http://www.rubyonrailsseminar.com/"&gt;Ruby on Rails seminar&lt;/a&gt; at &lt;a href="http://www.ajaxworldexpo.com/"&gt;AjaxWorld&lt;/a&gt; in Santa Clara, California.&lt;/p&gt;


	&lt;p&gt;As promised, I wanted to post some &lt;a href="http://www.planetargon.com/files/~robby/presentation_rails_meets_legacy_world.html"&gt;the slides&lt;/a&gt; from my talk today.&lt;/p&gt;


	&lt;p&gt;A few weeks ago, I announced the &lt;a href="http://groups.google.com/group/rubyonrails-legacy"&gt;Ruby on Rails Legacy mailing list&lt;/a&gt;, which I also talked about in my talk. I invite you all to stop by and ask questions&amp;#8230; and help the community grow.&lt;/p&gt;


	&lt;p&gt;If there was one thing that I wanted to really express in my talk&amp;#8230; it was that &lt;em&gt;you&amp;#8217;re not alone&lt;/em&gt;. You&amp;#8217;re not the only one faced with a situation where you need to work around legacy conventions, opinions, and politics.&lt;/p&gt;


	&lt;p&gt;...you&amp;#8217;re&amp;#8230; not&amp;#8230; alone. ;-)&lt;/p&gt;


	&lt;p&gt;Heading out to go see James Adam talk about plugins&amp;#8230; !&lt;/p&gt;
</description>
      <pubDate>Tue, 03 Oct 2006 19:50:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c0ea413e-6bf8-4902-88a7-6b1d47a205a0</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/10/03/slides-rails-meets-the-legacy-world</link>
      <category>legacy</category>
      <category>databases</category>
      <category>ajaxworld</category>
      <category>slides</category>
      <category>talk</category>
      <category>rails</category>
      <category>development</category>
      <category>hope</category>
    </item>
    <item>
      <title>RailsConf, day 1</title>
      <description>&lt;h2&gt;Rails and Database Schemas&lt;/h2&gt;


	&lt;p&gt;This morning, David Thomas opened up the conference by pointing out three problems that Rails needs to solve. The one that hit home for me and my love of databases&amp;#8230; especially monolithic legacy ones&amp;#8230; was his first bullet, &amp;#8220;Data Integration.&amp;#8221; Natural keys, composites, automatic AR relationships with reflection, and non-database backends. He didn&amp;#8217;t mention stored procedures&amp;#8230; I&amp;#8217;ll have to ask him about that. ;-)&lt;/p&gt;


	&lt;h2&gt;Shared Hosting and Rails&lt;/h2&gt;


	&lt;p&gt;I&amp;#8217;m currently sitting in Topfunky&amp;#8217;s talk, &amp;#8220;Rails Deployment on Shared Hosting&amp;#8221;. He had a few funny slides&amp;#8230; yes&amp;#8230; deployment on shared hosting can be painful. We&amp;#8217;ve been working with our customers to ease this problem as much as possible by collaborating on the &lt;a href="http://docs.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt; Documentation Project&lt;/a&gt;. He also suggested that people consider a &lt;span class="caps"&gt;VPS&lt;/span&gt;, which is a very viable option&amp;#8230; if you have the time and patience to setup the server. The pains of regular shared hosting are a big concern especially if you have a mission critical business application. This is why we moved towards our &lt;a href="http://www.planetargon.com/rails_business_hosting.html"&gt;Rails Business Hosting&lt;/a&gt; plans and are working out the details for yet another step above in terms of cost and reliability. Stay tuned for more details&amp;#8230;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ll post more notes later&amp;#8230;&lt;/p&gt;
</description>
      <pubDate>Fri, 23 Jun 2006 16:22:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:be5ee047-63be-43de-9d5c-05cd9f8798af</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/06/23/railsconf-day-1</link>
      <category>railsconf</category>
      <category>planetargon</category>
      <category>conferences</category>
      <category>hosting</category>
      <category>databases</category>
      <category>rails</category>
    </item>
    <item>
      <title>PostgreSQL: An elephant wearing a hula skirt and I find it sexy</title>
      <description>&lt;p&gt;Last week, I gave a &lt;a href="http://www.robbyonrails.com/articles/2006/04/14/sneaking-rails-through-the-legacy-system"&gt;live presentation&lt;/a&gt; to ~250 people, which was basically me walking through the process of using &lt;a href="http://www.rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt; to talk to a &lt;em&gt;legacy&lt;/em&gt; database&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;. For my example, I used the &lt;a href="http://pgfoundry.org/projects/dbsamples"&gt;Dell &lt;span class="caps"&gt;DVD&lt;/span&gt; database&lt;/a&gt;... the PostgreSQL version. You can review some comments about my presentation by reviewing &lt;a href="http://blog.zenspider.com/archives/2006/04/canadaonrails_d_1.html"&gt;this entry&lt;/a&gt; on the blog of Ryan Davis. I wrapped quite a bit of the database in a few minutes and then showed what another 45 minutes of work could do with Rails. I&amp;#8217;ll tarball that code and post it online soon.&lt;/p&gt;


	&lt;p&gt;Alex Bunardzic &lt;a href="http://jooto.com/blog/index.php/2006/04/19/practical-aspects-of-canada-on-rails/"&gt;found it practical&lt;/a&gt;. I&amp;#8217;ll take that as a compliment. ;-)&lt;/p&gt;


	&lt;p&gt;Earlier, I noticed &lt;a href="http://cwilliams.textdriven.com:8002/articles/2006/04/20/ruby-rails-and-web-development-drving-significant-book-sales"&gt;this blog entry&lt;/a&gt; by Chris&amp;#8230; where he said, &amp;#8220;PostgreSQL also seems to be growing more briskly among the database category, while MySQL declines modestly. I guess that&amp;#8217;s good news for Robby.&amp;#8221; What exactly made me happy? This bullet made by Tim O&amp;#8217;Reilly on Radar in his post, &lt;a href="http://radar.oreilly.com/archives/2006/04/state_of_the_computer_book_mar_3.html"&gt;State of the Computer Book Market, Part 2&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;&amp;#8220;A surprise to many may be the s&lt;strong&gt;trong growth of &lt;a href="http://www.postgresql.orgf"&gt;PostgreSQL&lt;/a&gt;, up 84% over a year ago&lt;/strong&gt;. We&amp;#8217;ve also been hearing some signs of growth in the Postgres market from our &amp;#8220;alpha geek&amp;#8221; radar, with reasons given including better support for geo data, and better handling of very large data sets. New companies like Greenplum and EnterpriseDB have also brought a little focus to this market. We&amp;#8217;re updating our &lt;strong&gt;PostgreSQL&lt;/strong&gt; book, and watching this market closely.&amp;#8221;&lt;/em&gt;&lt;/p&gt;


	&lt;p&gt;That&amp;#8217;s awesome! Go PostgreSQL!&lt;/p&gt;


	&lt;p&gt;Then to my surprise, I was contacted by &lt;span class="caps"&gt;CRN&lt;/span&gt; (again) to get my thoughts on &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/blackhole-storage-engine.html"&gt;MySQL&amp;#8217;s awesome &lt;em&gt;storage-engine plugin&lt;/em&gt; system&lt;/a&gt;... which showed up a few hours later in &lt;a href="http://crn.com/sections/breakingnews/breakingnews.jhtml?articleId=186500172"&gt;this article&lt;/a&gt;. The writer of that article managed to goof my last name (Robby Hill?)... and hopefully that gets resolved soon. ;-)&lt;/p&gt;


	&lt;p&gt;As I said at &lt;a href="http://www.canadaonrails.org"&gt;Canada on Rails&lt;/a&gt; in front of 250 people, &amp;#8220;I find databases&amp;#8230; sexy.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.planetargon.com/files/~robby/pg_pink_elephant.jpg"&gt;&lt;img src="http://www.planetargon.com/files/~robby/pg_pink_elephant-thumb.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;(free desktop wallpaper!)&lt;/p&gt;


	&lt;p&gt;Why Rails? Why PostgreSQL?&lt;/p&gt;


	&lt;p&gt;Rails.. that&amp;#8217;s a no-brainer.&lt;/p&gt;


	&lt;p&gt;PostgreSQL? Well.. they have compatible licenses&amp;#8230; no dual-license smell when your trying to sell your application as a complete solution&amp;#8230; and as &lt;a href="http://www.robbyonrails.com/articles/2005/09/27/the-bitter-sweet-taste-of-agnostic-database-schemas"&gt;Rails is database agnostic&lt;/a&gt;... there is no reason not to give PostgreSQL a try.&lt;/p&gt;


	&lt;p&gt;Who uses PostgreSQL on Rails?&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.cdbaby.com"&gt;CDBaby&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.shopify.com"&gt;Shopify&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; (on almost every project we work on&amp;#8230;)&lt;/li&gt;
		&lt;li&gt;you&amp;#8230;?&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;If your using PostgreSQL on Rails&amp;#8230; &lt;a href="mailto:robby@planetargon.com"&gt;email me&lt;/a&gt;... I&amp;#8217;d love to hear and share your story on my O&amp;#8217;Reilly blog, like I did with Derek Sivers and Jeremy Kemper of &lt;a href="http://www.cdbaby.com"&gt;CDBaby&lt;/a&gt; a few months ago, which you can &lt;a href="http://www.oreillynet.com/pub/wlg/8274"&gt;read here&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;again&amp;#8230; &lt;a href="http://noodlejunkie.com/?p=23"&gt;I find databases sexy&lt;/a&gt;...&lt;/p&gt;


	&lt;p&gt;If your a &lt;strong&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/strong&gt; hosting customer&amp;#8230; you can follow &lt;a href="http://docs.planetargon.com/wiki/show/Setup+A+PostgreSQL+Database"&gt;these instructions&lt;/a&gt; to install PostgreSQL on your &lt;a href="http://www.planetargon.com/hosting.html"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt; hosting&lt;/a&gt; account.&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; According to the Rails convention (and by me)... any database created pre-Rails or doesn&amp;#8217;t follow the conventions is considered legacy. ;-)&lt;/p&gt;
</description>
      <pubDate>Thu, 20 Apr 2006 18:30:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:363a8c6f-7b8c-462e-b91f-fb9b5d5e5c50</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/04/20/postgresql-an-elephant-wearing-a-hula-skirt-and-i-find-it-sexy</link>
      <category>postgresql</category>
      <category>programming</category>
      <category>databases</category>
    </item>
    <item>
      <title>Ruby on Rails, Refactoring Databases, and the Legacy System</title>
      <description>&lt;p&gt;To avoid feeling like I have neglected my blog lately&amp;#8230; I wanted to point out a few things.&lt;/p&gt;


	&lt;p&gt;First of all&amp;#8230; there is a nice new book that was sitting at &lt;a href="http://www.powells.com/"&gt;Powells&lt;/a&gt; technical bookstore just a few hours ago&amp;#8230; and is now sitting on my desk. I read a few pages on the bus ride home today of &lt;a href="http://www.powells.com/biblio/71-0321293533-0"&gt;Refactoring Database: Evolutionary Database Design&lt;/a&gt;. It&amp;#8217;s a &lt;a href="http://www.martinfowler.com/"&gt;Martin Fowler&lt;/a&gt; signature book and you can read more by Scott W. Ambler (one of the authors) at &lt;a href="http://www.agiledata.org"&gt;Agile Data&lt;/a&gt;. So far it seems like a good book if you&amp;#8217;re into database schemas and if your a fan of the &lt;a href="http://www.refactoring.com/"&gt;Refactoring&lt;/a&gt; scene. :-)&lt;/p&gt;


	&lt;p&gt;In other news&amp;#8230; &lt;a href="http://jvoorhis.com"&gt;Jeremy&lt;/a&gt; and I are preparing for our journey up to Vancouver, B.C. this week. Jeremy will be presenting his talk about i18n and Ruby on Rails&amp;#8230; and I&amp;#8217;ll be presenting my talk about using Ruby on Rails with Legacy databases. I look forward to meeting those of you who are making the trip to &lt;a href="http://www.canadaonrails.org"&gt;Canada on Rails&lt;/a&gt; (which is &lt;strong&gt;sold-out&lt;/strong&gt;!) and if you don&amp;#8217;t catch us there&amp;#8230; we will also be presenting at &lt;a href="http://railsconf.org"&gt;RailsConf 2006&lt;/a&gt;!&lt;/p&gt;


	&lt;p&gt;If you&amp;#8217;re going to be in Vancouver this week and would like to meet us for some drinks, hacking, or whatever&amp;#8230; stop by on &lt;strong&gt;#canadaonrails&lt;/strong&gt; (irc.freenode.net) and let us know. :-)&lt;/p&gt;


	&lt;h3&gt;Related Post(s)&lt;/h3&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.robbyonrails.com/articles/2005/07/25/the-legacy-of-databases-with-rails"&gt;The legacy of databases with Rails&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://robbyonrails.com/articles/2005/09/27/the-bitter-sweet-taste-of-agnostic-database-schemas"&gt;The bitter-sweat taste of agnostic database schemas&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
</description>
      <pubDate>Mon, 10 Apr 2006 20:11:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4a46ea3d-3451-44bf-af61-8e3cb7ed9da6</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/04/10/ruby-on-rails-refactoring-databases-and-the-legacy-system</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PostgreSQL</category>
      <category>rails</category>
      <category>refactoring</category>
      <category>databases</category>
      <category>legacy</category>
      <category>canada</category>
    </item>
    <item>
      <title>New Active Record Options for Associations</title>
      <description>&lt;p&gt;Two months ago today I posted about a bug in Active Record. A bug that reminded me to remain cautious about how much trust I put into a database abstraction layer. I am happy to now say  that this particular bug has been fixed, and I got to help! In the process, I also got to add some new features. (see my original rant, &lt;a href="http://www.robbyonrails.com/articles/2005/08/18/active-record-i-3-u-but-i-still-trust-my-database-server-a-tiny-bit-more"&gt;Active Record, I love U but I still trust my database server a tiny bit more&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;I discovered this bug when I was working on a chapter in &lt;a href="http://www.programmingrails.com"&gt;my book&lt;/a&gt; on Active Record. I&amp;#8217;m known to gladly take advantage of database constraints and triggers, and it was when I decided to test my code &lt;strong&gt;without&lt;/strong&gt; these constraints, I discovered the bug. &lt;em&gt;&amp;#8220;Hey, Active Record isn&amp;#8217;t doing what it&amp;#8217;s supposed to!&amp;#8221;&lt;/em&gt; I&amp;#8217;ve since had a number of people ask me if what a more pragmatic way to work around this issue is, rather than go my route by adding a constraint/trigger.&lt;/p&gt;


	&lt;p&gt;You can now &lt;strong&gt;&lt;span class="caps"&gt;DO AWAY&lt;/span&gt;&lt;/strong&gt; with &lt;strong&gt;&lt;code&gt;ON DELETE CASCADE&lt;/code&gt;!&lt;/strong&gt; (sort of)&lt;/p&gt;


	&lt;p&gt;The new release of Rails 1.0 Release Candidate includes some new options for the &lt;code&gt;has_many&lt;/code&gt; and &lt;code&gt;has_one&lt;/code&gt; declarations.&lt;/p&gt;


	&lt;p&gt;Previously, you could do the following:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Customer&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_many&lt;/span&gt; &lt;span class="symbol"&gt;:orders&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;This was supposed to nullify the dependent records, but &lt;a href="http://dev.rubyonrails.com/ticket/2009"&gt;it didn&amp;#8217;t&lt;/a&gt;!&lt;/p&gt;


	&lt;p&gt;My patch not only fixes this, but also gives more control with what &lt;cod&gt;:dependent&lt;/code&gt; does. Now, you can pass the &lt;code&gt;:dependent&lt;/code&gt; option to the &lt;code&gt;has_many&lt;/code&gt; and &lt;code&gt;has_one&lt;/code&gt; declarations with either &lt;code&gt;:nullify&lt;/code&gt; or &lt;code&gt;:destroy&lt;/code&gt;. This has a similar affect as &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; in those fancy RDBMs like PostgreSQL.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s take a closer look at these new options:&lt;/p&gt;


	&lt;h2&gt;has_one&lt;/h2&gt;


	&lt;p&gt;A spider &lt;code&gt;has_one&lt;/code&gt; web, and the web &lt;code&gt;belongs_to&lt;/code&gt; one spider. If you destroy the spider, you would most likely want to destroy the web as well.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Spider&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_one&lt;/span&gt; &lt;span class="symbol"&gt;:web&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:destroy&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;On the other hand, in the case of a snail that &lt;code&gt;has_one&lt;/code&gt; shell (and the one shell &lt;code&gt;belongs_to&lt;/code&gt; the snail), if you destroy the snail, you may want to keep the shell. Remember, your crazy Aunt Ruby collects snail shells.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Snail&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_one&lt;/span&gt; &lt;span class="symbol"&gt;:shell&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:nullify&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Now, for every snail we destroy, the shells, though once dependent on the snail, are now available for Aunt Ruby.&lt;/p&gt;


	&lt;h2&gt;has_many&lt;/h2&gt;


	&lt;p&gt;The same rules apply to the &lt;code&gt;has_many&lt;/code&gt; association. Most people assume that if you destory a beehive that you would destroy all the bees. I&amp;#8217;d like to think that they wander around until they find a new beehive to join. So, we can &lt;code&gt;:nullify&lt;/code&gt; their relationship when the beehive is destroyed, thus making them homeless, but available for future hives.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Beehive&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_many&lt;/span&gt; &lt;span class="symbol"&gt;:bees&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:nullify&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Sadly, some people might want to destroy the bees along with the destruction of the beehive. So, &lt;em&gt;those people&lt;/em&gt; can pass the &lt;code&gt;:dependent&lt;/code&gt; option,  &lt;code&gt;:destroy&lt;/code&gt;.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Beehive&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;ActiveRecord&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Base&lt;/span&gt;
  &lt;span class="ident"&gt;has_many&lt;/span&gt; &lt;span class="symbol"&gt;:bees&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:dependent&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="symbol"&gt;:destroy&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Don&amp;#8217;t worry, your usage of &lt;code&gt;:dependent =&amp;gt; true&lt;/code&gt; will now work, even though it wasn&amp;#8217;t working before.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m going to try to put more trust into Active Record now, and I hope that this new addition to the library finds itself useful for you. :-)&lt;/p&gt;


	&lt;p&gt;Take a peak at the cool &lt;a href="http://documentation.rubyonrails.com/release_notes/rc2.html"&gt;new features&lt;/a&gt; in the latest version of Rails and see the &lt;a href="http://api.rubyonrails.com/files/vendor/rails/activerecord/CHANGELOG.html"&gt;Active Record &lt;span class="caps"&gt;CHANGELOG&lt;/span&gt;&lt;/a&gt; for more information.&lt;/p&gt;


	&lt;p&gt;Enjoy!&lt;/p&gt;
</description>
      <pubDate>Wed, 19 Oct 2005 18:00:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:c9cc19af4007d50c22cf5dbfb39c280c</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/19/new-active-record-options-for-associations</link>
      <category>Ruby on Rails</category>
      <category>Programming</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>databases</category>
      <category>postgresql</category>
    </item>
  </channel>
</rss>
