<?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 activerecord</title>
    <link>http://www.robbyonrails.com/articles/tag/activerecord</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Master/Slave Databases with Ruby on Rails</title>
      <description>&lt;p&gt;Not terribly long ago, I &lt;a href="http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails"&gt;announced Active Delegate&lt;/a&gt;, which was a really lightweight plugin that I developed to allow models to talk to multiple databases for specific methods. The plugin worked great for really simple situations, like individual models.. but when it came time to test with associations it fell apart. I haven&amp;#8217;t had a chance to work on any updates and knew that it was going to take more work to get it going.&lt;/p&gt;


	&lt;p&gt;Earlier this week, we helped one of our bigger clients launch their new web site&lt;sup&gt;&lt;a href="#fn1"&gt;1&lt;/a&gt;&lt;/sup&gt;. For the deployment, we needed to send all writes to a master database and a reads to slaves (initial deployment is talking to almost 10 slaves spread around the globe!). We needed something to get integrated quickly and decided to ditch Active Delegate for the time being and began looking at the following options.&lt;/p&gt;


	&lt;p&gt;I spoke with Rick Olson&lt;sup&gt;&lt;a href="#fn2"&gt;2&lt;/a&gt;&lt;/sup&gt; and he pointed me to a new plugin that he hasn&amp;#8217;t really released yet. So, I&amp;#8217;m going to do him a favor and announce it for him. Of course&amp;#8230; I got his permission first&amp;#8230; ;-)&lt;/p&gt;


	&lt;h2&gt;Announcing Masochism!&lt;/h2&gt;


	&lt;p&gt;Masochism&lt;sup&gt;&lt;a href="#fn3"&gt;3&lt;/a&gt;&lt;/sup&gt; is a new plugin for Ruby on Rails that allows you to delegate all writes to a master database and reads to a slave database. The configuration process is just a few lines in your environment file and the plugin takes care of the rest.&lt;/p&gt;


	&lt;h3&gt;Installing Masochism&lt;/h3&gt;


	&lt;p&gt;With &lt;a href="http://piston.rubyforge.org/usage.html"&gt;piston&lt;/a&gt;, you can import Masochism with:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  $ cd vendor/plugins
  $ piston import http://ar-code.svn.engineyard.com/plugins/masochism/
&lt;/code&gt;&lt;/pre&gt;

	&lt;ul&gt;
	&lt;li&gt;To learn more about piston, read &lt;a href="http://www.robbyonrails.com/articles/2007/01/16/every-second-counts-with-a-piston-in-your-trunk"&gt;Every Second Counts with a Piston in your trunk&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;You can also install it with the old-fashioned way:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  $ ./script/plugin install -x http://ar-code.svn.engineyard.com/plugins/masochism/
&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Configuring Masochism&lt;/h3&gt;


	&lt;p&gt;The first thing that you&amp;#8217;ll need to do is add another database connection in &lt;code&gt;config/database.yml&lt;/code&gt; for &lt;code&gt;master_database&lt;/code&gt;. By default, Masochism expects you to have a production database, which will be the read-only/slave database. The &lt;code&gt;master_database&lt;/code&gt; will be the connection details for your (you guessed it&amp;#8230;) master database.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# config/database.yml  
production:
  database: masochism_slave_database
  adapter: postgresql
  host: slavedb1.hostname.tld
  ...

master_database:
  database: masochism_master_database
  adapter: postgresql
  host: masterdb.hostname.tld
  ...
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The idea here is that replication will be handled elsewhere and your application can reap the benefits of talking to the slave database for all of it&amp;#8217;s read-only operations and let the master database(s) spend their time writing data.&lt;/p&gt;


	&lt;p&gt;The next step is to set this up in your environment file. In our scenario, this was &lt;code&gt;config/environments/production.rb&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# Add this to config/environments/production.rb
config.after_initialize do 
  ActiveReload::ConnectionProxy.setup!    
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Voila, you should be good to go now. As I mentioned, we&amp;#8217;ve only been using this for this past week and we&amp;#8217;ve had to address a few problems that the initial version of the plugin didn&amp;#8217;t address. One of our developers, &lt;a href="http://andy.delcambre.com/"&gt;Andy Delcambre&lt;/a&gt;, just posted an article to show how we had a problem with &lt;a href="http://andy.delcambre.com/2007/11/15/masochistic-connection-proxy-with-observers"&gt;using ActiveRecord observers and masochism&lt;/a&gt;, which we&amp;#8217;re sending over a patch for now.&lt;/p&gt;


	&lt;p&gt;As we continue to monitor how this solution works, we&amp;#8217;ll report any findings on our blog. In the meantime, I&amp;#8217;d be interested in knowing what you&amp;#8217;re using to solve this problem. :-)&lt;/p&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; &lt;a href="http://contiki.com"&gt;Contiki&lt;/a&gt;, a cool travel company we&amp;#8217;re working with&lt;/p&gt;


	&lt;p id="fn2"&gt;&lt;sup&gt;2&lt;/sup&gt; Rick just moved to Portland&amp;#8230; welcome to stump town!&lt;/p&gt;


	&lt;p id="fn3"&gt;&lt;sup&gt;3&lt;/sup&gt; &lt;a href="http://ar-code.svn.engineyard.com/plugins/masochism/README"&gt;The Masochism plugin &lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;
</description>
      <pubDate>Thu, 15 Nov 2007 16:02:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3420b2e3-a80c-43c9-a136-a58040069607</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/11/15/master-slave-databases-with-ruby-on-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>plugins</category>
      <category>masochism</category>
      <category>database</category>
      <category>mysql</category>
      <category>postgresql</category>
      <category>client</category>
      <category>development</category>
      <category>activerecord</category>
      <category>replication</category>
      <category>deployment</category>
    </item>
    <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>Q&amp;amp;A: ActiveRecord Observers and You</title>
      <description>&lt;p&gt;Yesterday, I wrote a short post titled, &lt;a href="http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small"&gt;Observers Big and Small&lt;/a&gt;, about using Observers in your Rails applications.&lt;/p&gt;


	&lt;p&gt;The following questions were raised in the comments.&lt;/p&gt;


	&lt;h2&gt;When should I use an Observer?&lt;/h2&gt;


	&lt;p&gt;&lt;a href="http://www.last100meters.com/"&gt;Eric Allam&lt;/a&gt; asks&amp;#8230;&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;&amp;#8220;Why not just use ActiveRecord callback hooks instead of Observers? Are Observers more powerful or is it just a matter of preference?&amp;#8221;&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Eric, this is an excellent question. I&amp;#8217;d say that a majority of the time, using the &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html"&gt;ActiveRecord callbacks&lt;/a&gt; in your models is going to work for your situation. However, there are times that you want the same methods to be called through callbacks. For example, let&amp;#8217;s take a recent problem that we used an observer to solve.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://blog.imperialdune.com/"&gt;Graeme&lt;/a&gt; is working on implementing &lt;a href="http://rubyforge.org/projects/ferret"&gt;Ferret&lt;/a&gt; into a project that we&amp;#8217;re developing for a client. With the use of Ferret, we can index and later search through content over several objects into a format that makes sense for our implementation goals. Each time an object is created and updated, we have to update our Ferret indexes to reflect these changes. The most obvious location that we can call our indexing methods is in each models&amp;#8217; callbacks, but this violates the &lt;span class="caps"&gt;DRY&lt;/span&gt;[1] principle. So, we created an Observer, which &lt;em&gt;observes&lt;/em&gt; each of the models that need these methods to be called. In fact, as far as we&amp;#8217;re concerned, the fact that we&amp;#8217;re indexing some of its data, is none of its business. We only want our models to be concerned with that they&amp;#8217;re designed to be concerned about. We may opt to change our indexing solution in the future and we&amp;#8217;d just need to rethink that at the Observer level and not change anything about the business logic in our models.&lt;/p&gt;


	&lt;p&gt;This is the sort of scenario when using an Observer makes great sense in your application.&lt;/p&gt;


	&lt;h2&gt;Logging from an Observer&lt;/h2&gt;


	&lt;p&gt;Adam R. asks&amp;#8230;&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;&lt;em&gt;&amp;#8220;I&amp;#8217;d also like the ability to use the logger from within an observer, but that’s another issue.&amp;#8221;&lt;/em&gt;&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;I assume that you are referring to the &lt;code&gt;logger&lt;/code&gt; method? I always forget to even use that method. I do know that the following works just fine in an Observer.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class IndexObserver &amp;lt; ActiveRecord::Observer
  observer Article, Editorial, BlogPost, ClassifiedAd

  def after_save(model)
    RAILS_DEFAULT_LOGGER.warn("Every single day. Every word you say. Every game you play. Every night you stay. I'll be watching you.")
    # execute something fun
  end
end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This will output to your log file without any problem.&lt;/p&gt;


	&lt;p&gt;This reminded me of when I used to want to &lt;a href="http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests"&gt;log from Unit Tests&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;(few minutes later)&lt;/p&gt;


	&lt;p&gt;Okay, I just attempted to use &lt;code&gt;logger&lt;/code&gt; from an Observer and you&amp;#8217;re right&amp;#8230; it doesn&amp;#8217;t currently work. There is a simple fix though, just extend ActiveRecord::Observer to add a &lt;code&gt;logger&lt;/code&gt; method like so and require it in &lt;code&gt;config/environment.rb&lt;/code&gt; (much like I did in with unit tests).&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# lib/observer_extensions.rb
class ActiveRecord::Observer
  def logger
    RAILS_DEFAULT_LOGGER
  end
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This will give you a solution to that problem.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
class FooObserver &amp;lt; ActiveRecord::Observer
  observer Foo

  def after_save(model)
    logger.warn("I wonder if the #{address.class} knows that I've been watching it all along?")
  end
end  
&lt;/code&gt;&lt;/pre&gt;

	&lt;h2&gt;Observers Spy for Us&lt;/h2&gt;


	&lt;p&gt;Most often, I look at Observers as being the guys that I hire to spy on my models. I don&amp;#8217;t want my models to know that they&amp;#8217;re being spied on and I&amp;#8217;d like to keep it that way. They don&amp;#8217;t solve all of our problems and it&amp;#8217;s easy to overuse them. However, I have found several cases that they made a lot of sense and most of those cases have been where we&amp;#8217;ve had the same things occurring in our model&amp;#8217;s callbacks.&lt;/p&gt;


	&lt;p&gt;If you have other questions related to Observers, feel free to let me know. If you&amp;#8217;re already using Observers, perhaps you could post a comment and/or blog post response with an example of when and how you use Observers in your Rails applications.&lt;/p&gt;


	&lt;h3&gt;Related Posts&lt;/h3&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small"&gt;Observers Big and Small&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go"&gt;Where did my Observer go?&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href="http://tom-eric.info/archive/2006/10/30/observers-in-ruby-on-rails"&gt;Observers in Ruby on Rails&lt;/a&gt;, Tom Eric&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p id="fn1"&gt;&lt;sup&gt;1&lt;/sup&gt; Don&amp;#8217;t Repeat Yourself&lt;/p&gt;
</description>
      <pubDate>Sat, 28 Apr 2007 17:28:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:587732a9-50fb-476d-b46a-1c10d29c3559</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/04/28/q-a-activerecord-observers-and-you</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PLANET ARGON</category>
      <category>activerecord</category>
      <category>observers</category>
      <category>patterns</category>
      <category>q</category>
      <category>a</category>
      <category>logging</category>
      <category>ferret</category>
      <category>graeme</category>
      <category>DRY</category>
    </item>
    <item>
      <title>Observers Big and Small</title>
      <description>&lt;p&gt;My colleague, &lt;a href="http://blog.garyblessington.us"&gt;Gary&lt;/a&gt;, keeps a stack of Ruby and Rails books on his desk and was implementing an Observer into a client project. It appears that the Agile Web Development with Rails book is still encouraging people to do the following in order to load an Observer.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# app/models/flower_observer.rb
class FlowerObserver &amp;lt; ActiveRecord::Observer
  observe Flower

  def after_create(model)
    # model.do_something!
  end
end

# controller(s)
class FlowerController &amp;lt; ApplicationController
  observer :flower_observer
end
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;What is wrong with this approach?&lt;/p&gt;


	&lt;p&gt;Well, in order for your Observer to be used, the model(s) callbacks that it is observing need to be triggered through a controller. If you end up writing any scheduled rake tasks, your observer will not be called. In my opinion, the controller shouldn&amp;#8217;t know this much about the model. In fact, the model doesn&amp;#8217;t even really know about it&amp;#8217;s observer&amp;#8230; so why should a controller?&lt;/p&gt;


	&lt;p&gt;This was actually changed a long time ago (&lt;a href="http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go"&gt;I previously blogged about a different solution here&lt;/a&gt;) and the Rails docs for &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Observer.html"&gt;ActiveRecord::Observer&lt;/a&gt; are currently correct.&lt;/p&gt;


	&lt;h2&gt;Observers in the Environment&lt;/h2&gt;


	&lt;p&gt;If you open up a recent version of &lt;code&gt;config/environment.rb&lt;/code&gt;, you notice in the comments the following.&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
  # Activate observers that should always be running
  # config.active_record.observers = :cacher, :garbage_collector
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Take a moment to go ahead and specify which observer(s) you&amp;#8217;d like to load into your Rails environment.&lt;/p&gt;


&lt;pre&gt;config.active_record.observers = :flower_observer&lt;/pre&gt;

	&lt;p&gt;Then you can remove your observer calls in all your controllers, because that&amp;#8217;s not where you should be defining them.&lt;/p&gt;


	&lt;p&gt;Also, if you&amp;#8217;re not using Observers yet, I&amp;#8217;d really encourage you to consider reading up on them and giving them a try.&lt;/p&gt;
</description>
      <pubDate>Fri, 27 Apr 2007 13:15:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:ebff46c4-4af0-4e95-b950-72023fff7d0a</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2007/04/27/observers-big-and-small</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>activerecord</category>
      <category>development</category>
      <category>rails</category>
      <category>models</category>
      <category>observers</category>
      <category>docs</category>
    </item>
    <item>
      <title>Where did my observer go?</title>
      <description>&lt;p&gt;I was playing around with an application from &lt;code&gt;script/console&lt;/code&gt; and all of a sudden&amp;#8230; my &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Observer.html"&gt;Observers&lt;/a&gt; just stopped working. It took me a while to figure out that when I would call &lt;code&gt;Dispatcher.reset_application!&lt;/code&gt;... it wouldn&amp;#8217;t reload config/environment.rb where I have defined the observers.&lt;/p&gt;


	&lt;p&gt;I really don&amp;#8217;t like that this is the current implementation for invoking Observers and adding them to the controllers is not kosher with my (do-it-in-script-console) approach.&lt;/p&gt;


	&lt;p&gt;The current solution?&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
# app/models/foo_bar.rb
class FooBar &amp;lt; ActiveRecord::Base
end

FooBarObserver.instance
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;... there needs to be a better way!&lt;/p&gt;
</description>
      <pubDate>Mon, 27 Feb 2006 23:05:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:bf47590a-2ccc-4c32-87a6-7b45325f630f</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/02/27/where-did-my-observer-go</link>
      <category>observers</category>
      <category>activerecord</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails Migrations and PostgreSQL Constraints</title>
      <description>&lt;p&gt;A question was posed on the Rails mailing list concerning how one would go about adding CONSTRAINTs to the database tables with &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/Migration.html"&gt;ActiveRecord::Migration&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;One argument was raised stating that it is easier to handle these in plain &lt;span class="caps"&gt;SQL&lt;/span&gt; schema files. I disagree. :-)&lt;/p&gt;


	&lt;h3&gt;Migrations to the Rescue&lt;/h3&gt;


	&lt;p&gt;Databases evolve and I have recently found the Migration structure to be perfect for handling iterations and schema changes. Using the &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000529"&gt;#execute&lt;/a&gt; method has helped move more of my code into the Ruby/Rails framework&amp;#8230; and that just makes things easier to manage in the long-run. This is the approach that we are using at &lt;a href="http://www.planetargon.com/development.html"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; with some of our current client projects.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# db/migrate/6_add_foreign_key.rb&lt;/span&gt;
&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;AddForeignKey&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;Migration&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.up&lt;/span&gt;
    &lt;span class="ident"&gt;execute&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;ALTER TABLE bees ADD CONSTRAINT beehive_id_fkey FOREIGN KEY
(beehive_id) REFERENCES beehives (id);&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;self.down&lt;/span&gt;
    &lt;span class="ident"&gt;execute&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;ALTER TABLE bees DROP CONSTRAINT beehive_id_fkey;&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;
  &lt;span class="keyword"&gt;end&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 gives us an easy way to use the standard, &lt;a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#M000507"&gt;#create_table&lt;/a&gt; syntax for building our tables with Ruby&amp;#8230; and then we can slap these constraints on later.&lt;/p&gt;


	&lt;p&gt;This would add the constraints&amp;#8230;&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
rake migrate VERSION=6
&lt;/pre&gt;
&lt;/code&gt;

...run tests&amp;#8230;
&lt;code&gt;
&lt;pre&gt;
rake
&lt;/pre&gt;
&lt;/code&gt;

...roll back&amp;#8230;
&lt;code&gt;
&lt;pre&gt;
rake migrate VERSION=5
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;I have found that this approach is really useful with testing in Rails. When I think that I have everything working great (without &lt;span class="caps"&gt;CONSTRAINTS&lt;/span&gt; in PostgreSQL), I run another migration to add a bunch of foreign key and data constraints to the tables and&amp;#8230; run my tests again.&lt;/p&gt;


	&lt;h3&gt;Let&amp;#8217;s give Active Record a Hug&lt;/h3&gt;


	&lt;p&gt;This has helped me &lt;a href="http://www.robbyonrails.com/articles/2005/09/27/the-bitter-sweet-taste-of-agnostic-database-schemas"&gt;gain some trust&lt;/a&gt; in Active Record while still giving me that comforting feeling that &lt;a href="http://www.postgresql.org"&gt;PostgreSQL&lt;/a&gt; is acting as the &lt;em&gt;body guard for my data&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Even if you don&amp;#8217;t end up using Migrations to handle these types of database schema changes, I would highly suggest that you model your implementation after this. I&amp;#8217;ve worked with many database schemas and this just makes it easy to add your new change and run one command to commit it to the database.&lt;/p&gt;


	&lt;p&gt;...and now I go play with beehives&amp;#8230;&lt;/p&gt;
</description>
      <pubDate>Fri, 11 Nov 2005 10:42:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f125682d520f030e69dadb4be8913ebe</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/11/11/rails-migrations-and-postgresql-constraints</link>
      <category>Ruby on Rails</category>
      <category>Programming</category>
      <category>rails</category>
      <category>postgresql</category>
      <category>migrations</category>
      <category>database</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>Einstein on Rails</title>
      <description>&lt;p&gt;Einstein would like Active Record&amp;#8230; I think&amp;#8230;&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://www.planetargon.com/files/~robby/einstein_ar.jpg" alt="" /&gt;&lt;/p&gt;


	&lt;p&gt;Have a great weekend&amp;#8230; !&lt;/p&gt;
</description>
      <pubDate>Fri, 04 Nov 2005 18:59:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:261bf7eb474b2819cc5188621ab0d865</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/11/04/einstein-on-rails</link>
      <category>Ruby on Rails</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>Migrating from MySQL to PostgreSQL in 60 seconds (with Rails)</title>
      <description>&lt;p&gt;I do quite a few MySQL to PostgreSQL migrations for clients of &lt;a href="http://www.planetargon.com"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt; and over the years I have gone through different methods of handling this procedure. Typically, it involved exporting data, re-importing it through some shell script that does its best to take into consideration the slight differences between the two databases.&lt;/p&gt;


	&lt;p&gt;While working on a recent project that was to be migrated from &lt;a href="http://sql-info.de/mysql/gotchas.html"&gt;MySQL&lt;/a&gt; to &lt;a href="http://www.postgresql.org"&gt;PostgreSQL&lt;/a&gt;, but after some &lt;a href="http://www.refactoringrails.com"&gt;Refactoring&lt;/a&gt; had been done to the code base. I had been meaning to spend a few hours coming up with some simplified process of taking data from MySQL and replicating it to a new PostgreSQL database, while maintaining  referential integrity.&lt;/p&gt;


	&lt;p&gt;My original idea was to build a script that referenced two seperate databases and then copied data out and inserted it into the new db after performing a few data changes. This seemed like too much work and I knew that I should be able to harness the power of Ruby and Rails in my process. My next thought? &lt;a href="http://redhanded.hobix.com/inspect/railsfsAfterACoupleMinutesOfToolingWithFuseWhoa.html"&gt;RailsFS&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;In a &lt;strong&gt;perfect&lt;/strong&gt; world, I would mount one instance of the application with a mysql database and another with a postgresql database&amp;#8230; and just copy the yaml files to the postgresql and bam, it&amp;#8217;d work, right? Well, it doesn&amp;#8217;t quite work that way. So, I ruled out that idea.&lt;/p&gt;


	&lt;p&gt;Then I had another idea. I would build a task in my Rakefile that&amp;#8230; imported each of those &lt;span class="caps"&gt;YAML&lt;/span&gt; files into PostgreSQL!&lt;/p&gt;


	&lt;p&gt;We can now type in the following commands:&lt;/p&gt;


This first creates my PostgreSQL database tables using Migrations.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;migrate&lt;/span&gt; &lt;span class="constant"&gt;VERSION&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;then&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;mysql2pgsql&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;code&gt;mysql2pgsql&lt;/code&gt; runs&amp;#8230;and approx 60 seconds later, I had an exact copy of the MySQL database in PostgreSQL. 

But wait! PostgreSQL has &lt;span class="caps"&gt;SEQUENCES&lt;/span&gt;&amp;#8230; so I then run:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;rake&lt;/span&gt; &lt;span class="ident"&gt;migrate&lt;/span&gt; &lt;span class="constant"&gt;VERSION&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;...and my &lt;span class="caps"&gt;SEQUENCES&lt;/span&gt; are &lt;span class="caps"&gt;ALTER&lt;/span&gt;&amp;#8217;d.&lt;/p&gt;


	&lt;p&gt;Yes, I will post some code in the near future. But all I am doing is basically running through all the &lt;span class="caps"&gt;YAML&lt;/span&gt; files that are available from RailsFS and adding them into the new database. It automatically figures out the model name&amp;#8230;and is fairly generic at the moment. However, I&amp;#8217;d like to spend a bit more time stress-testing it before I post it. :-)&lt;/p&gt;


	&lt;p&gt;I want to try to work on a RailsFS-less version as well so that I can run this on my PowerBook.&lt;/p&gt;


	&lt;p&gt;Until next time&amp;#8230; have fun!&lt;/p&gt;
</description>
      <pubDate>Sat, 29 Oct 2005 14:23:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:b1064070f97315072ad3be65a29dc575</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/29/migrating-from-mysql-to-postgresql-in-60-seconds-with-rails</link>
      <category>Ruby on Rails</category>
      <category>PostgreSQL</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>postgresql</category>
      <category>mysql</category>
      <category>fusefs</category>
    </item>
    <item>
      <title>Using Named Placeholders in Ruby</title>
      <description>&lt;p&gt;&lt;strong&gt;Insert Hip Quote Here:&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;&lt;em&gt;&amp;#8220;In ancient times, hundreds of years before the dawn of history, an ancient race of people&amp;#8230; the Druids. No one knows who they were or what they were doing&amp;#8230; &amp;#8220;&lt;/em&gt; &amp;#8211; Nigel Tufnel, Spinal Tap&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Story Time&amp;#8230;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Earlier, I was giving a customer of ours, Jared from &lt;a href="http://www.communitywalk.com"&gt;CommunityWalk.com&lt;/a&gt; a quick tutorial on some of the features script/console&amp;#8230; which lead to helping him with a &lt;span class="caps"&gt;SQL&lt;/span&gt; query. When I provided him with some working code he was curious about what I had done in the &lt;span class="caps"&gt;SQL&lt;/span&gt; query string that I was passing to &lt;code&gt;find_by_sql&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;WARNING&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;If you have &lt;span class="caps"&gt;ANY SQL&lt;/span&gt; queries that resembles the following, &lt;span class="caps"&gt;PLEASE READ THE REST OF THIS&lt;/span&gt;. :-)&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;params&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:search&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;
&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;first_name = '&lt;span class="expr"&gt;#{values['first_name']}&lt;/span&gt;'&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;If you are doing that&amp;#8230; then you are opening yourself up to some security problems. Let&amp;#8217;s take a few minutes and discuss how you can make this more secure and still keep your code readable. (the best of both worlds!)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;The ? Placeholder&lt;/strong&gt;&lt;/p&gt;


Many of you are probably familiar with this approach&amp;#8230;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;first_name = ?&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Nigel&lt;/span&gt;&lt;span class="punct"&gt;']&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;first_name = ? AND last_name = ?&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Nigel&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Tufnel&lt;/span&gt;&lt;span class="punct"&gt;']&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;You can pass it a hash as well.. and as long as you put everything in the same order as the &lt;code&gt;?&lt;/code&gt;s are placed&amp;#8230; then all is well.&lt;/p&gt;


	&lt;p&gt;My only real problem with this approach is that it requires you to keep things in a specific sequential order&amp;#8230; and who wants to keep track of that? So, I would like to recommend that you use named placeholders. Aside from that, it looks magical and I don&amp;#8217;t like magical-looking code. I like &lt;em&gt;easy to read code&lt;/em&gt;. :-)&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Named Placeholders&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;If you already use these&amp;#8230; you know how  useful they can be in your &lt;span class="caps"&gt;SQL&lt;/span&gt; queries. If you haven&amp;#8217;t seen them before&amp;#8230; it&amp;#8217;s because the Rails docs don&amp;#8217;t really mention it and is something that comes from the underlying database library in Ruby. So what is so great about these?&lt;/p&gt;


Let&amp;#8217;s first replace the above code with named parameters&amp;#8230;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;first_name = :first_name&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:first_name&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Nigel&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt; &lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;We are passing a hash with a matching key to the conditions option. Pretty neat, right? In this case with just one placeholder we just increased the amount of code to do the same thing. So, it might always be the best solution&amp;#8230; but it is &lt;em&gt;easier to read&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s try another with multiple keys in our hash&amp;#8230; infact, we&amp;#8217;ll build the hash prior to calling &lt;code&gt;find&lt;/code&gt;.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:first_name&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Nigel&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="symbol"&gt;:last_name&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Tufnel&lt;/span&gt;&lt;span class="punct"&gt;'}&lt;/span&gt;
&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;first_name = :first_name AND last_name = :last_name&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;It will happily match the hash keys to the named placeholders in the conditions string. Again, nothing terribly exciting&amp;#8230;but it is &lt;em&gt;easier to read&lt;/em&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Who cares about order? Not named placeholders!&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Okay, let&amp;#8217;s mix things up a bit&amp;#8230;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:last_name&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Tufnel&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="symbol"&gt;:first_name&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Nigel&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;
&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;first_name = :first_name AND last_name = :last_name&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The hash keys were not added in the same order&amp;#8230; but it still works!&lt;/p&gt;


	&lt;p&gt;Okay, now for one last quick example (it&amp;#8217;s late and I am tired&amp;#8230;).&lt;/p&gt;


	&lt;p&gt;I have a search mechanism on a site that allows you to search for a string of text across multiple fields. So, I have one string&amp;#8230; but several fields to compare against.&lt;/p&gt;


Here is a string that I will pass to the &lt;code&gt;find&lt;/code&gt; method.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;conditions&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;role = :role AND (first_name ~* :str OR last_name ~* :str OR nick_name ~* :str)&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: this string is using &lt;a href="http://rubyurl.com/DYE"&gt;PostgreSQL regular expressions&lt;/a&gt;... (&lt;code&gt;~*&lt;/code&gt;).&lt;/p&gt;


	&lt;p&gt;Here is a hash that with that matches the keys, &lt;code&gt;:str&lt;/code&gt; and &lt;code&gt;:role&lt;/code&gt;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt; &lt;span class="symbol"&gt;:str&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;^(Nigel|Tufnel)$&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="symbol"&gt;:role&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Guitar&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;If you look above, you&amp;#8217;ll see that the &lt;code&gt;conditions&lt;/code&gt; string contains &lt;strong&gt;four&lt;/strong&gt; named placeholders&amp;#8230; but the hash only has &lt;strong&gt;two&lt;/strong&gt; keys. With the &lt;code&gt;?&lt;/code&gt; placeholder, we would have to pass the same vaule three times&amp;#8230; which isn&amp;#8217;t any fun to read or maintain. ;-/&lt;/p&gt;


	&lt;p&gt;So, with our new friends, &lt;strong&gt;named parameters&lt;/strong&gt;, we can call &lt;code&gt;find&lt;/code&gt; (or any find-like method) using this technique for placeholders.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;RockLegend&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;find&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="symbol"&gt;:all&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:conditions&lt;/span&gt; &lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;[&lt;/span&gt; &lt;span class="ident"&gt;conditions&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;values&lt;/span&gt; &lt;span class="punct"&gt;]&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;...and hopefully this is useful to you.  :-)&lt;/p&gt;


	&lt;p&gt;Have fun!&lt;/p&gt;
</description>
      <pubDate>Fri, 21 Oct 2005 00:53:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4ffd337b75c41f3b73433e7919ddca5e</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>PostgreSQL</category>
      <category>activerecord</category>
      <category>rails</category>
      <category>ruby</category>
      <category>postgresql</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>
    <item>
      <title>Rails meets FuseFS</title>
      <description>&lt;p&gt;_why has &lt;a href="http://redhanded.hobix.com/inspect/railsfsAfterACoupleMinutesOfToolingWithFuseWhoa.html"&gt;posted an entry&lt;/a&gt; on his blog which shows how to use the &lt;a href="http://rubyforge.org/projects/fusefs"&gt;Ruby FuseFS&lt;/a&gt; library to&amp;#8230; mount Active Record.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
railsmnt]$ cat contact_type/1
--- !ruby/object:ContactType
attributes:
  name: Client
  id: "1" 

 railsmnt]$ for i in `ls`; do echo $i; done
contact
contact_type
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;Umm&amp;#8230; I am&amp;#8230; I dunno&amp;#8230; (wow)&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;ve used FuseFS for mounting &lt;span class="caps"&gt;SSH&lt;/span&gt;&amp;#8230; but found myself using &lt;span class="caps"&gt;SSHFS&lt;/span&gt; as a better alternative.&lt;/p&gt;


	&lt;p&gt;But&amp;#8230; to mount Rails? This could get interesting.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;A screenshot while browsing the directory with Konqueror&amp;#8230;and opening the record with Kedit.&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.robbyrussell.com/albums/Desktops/railsfs.jpg"&gt;&lt;img src="http://www.robbyrussell.com/albums/Desktops/railsfs.sized.jpg" alt="" /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt; #2&lt;/strong&gt;
If you&amp;#8217;re hosting at &lt;a href="http://www.planetargon.com/"&gt;&lt;span class="caps"&gt;PLANET ARGON&lt;/span&gt;&lt;/a&gt;, you can try this out with your account. Both, &lt;span class="caps"&gt;FUSE&lt;/span&gt; and Ruby-FuseFS are installed on all the servers.&lt;/p&gt;
</description>
      <pubDate>Wed, 21 Sep 2005 15:28:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:2be081b80bf04178fb8f56c9578a73d0</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/09/21/rails-meets-fusefs</link>
      <category>fusefs</category>
      <category>rails</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>PostgreSQL sequences in Rails</title>
      <description>&lt;p&gt;Rails doesn&amp;#8217;t support legacy or custom named &lt;a href="http://rubyurl.com/6Wh"&gt;sequences&lt;/a&gt; at the moment. (as far as I am aware). It&amp;#8217;s kind of tricky to have it detect the &lt;code&gt;SEQUENCE&lt;/code&gt; name automatically (every time).&lt;/p&gt;


In &lt;span class="caps"&gt;PHP&lt;/span&gt;, I used this big ugly query to detect this info:
&lt;code&gt;
&lt;pre&gt;
$sql = $db-&amp;gt;prepare("SELECT seq.relname::text
                        FROM pg_class src, pg_class seq, pg_namespace, pg_attribute,
                        pg_depend
                        WHERE
                            pg_depend.refobjsubid = pg_attribute.attnum AND
                            pg_depend.refobjid = src.oid AND
                            seq.oid = pg_depend.objid AND
                            src.relnamespace = pg_namespace.oid AND
                            pg_attribute.attrelid = src.oid AND
                            pg_namespace.nspname = ? AND
                            src.relname = ? AND
                            pg_attribute.attname = ?");
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;I used this to mimmick the mysql_insert_id function in &lt;span class="caps"&gt;PHP&lt;/span&gt; for PostgreSQL&amp;#8230; ( &lt;a href="http://blog.planetargon.com/archives/29-PHP-pg_insert_id.html"&gt;pg_insert_id&lt;/a&gt; )&lt;/p&gt;


	&lt;p&gt;Well, with Rails, I thought that I would build a similar patch, as the current code just assumes the value would be &lt;code&gt;{column}_id_seq&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;After hours of playing around and thinking that I &lt;a href="http://rubyurl.com/yKc"&gt;figured it all out&lt;/a&gt; ... I decided to run a quick test with a non standard sequence name&amp;#8230; like this one:&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
testingdb=# \d legacy.foobar
                                     Table "legacy.foobar" 
  Column   |         Type          |                         Modifiers                          
-----------+-----------------------+------------------------------------------------------------
 foobar_id | integer               | not null default nextval('legacy.old_sequence_name'::text)
 name      | character varying(40) | 
Indexes:
    "foobar_pkey" PRIMARY KEY, btree (foobar_id)

testingdb=# INSERT INTO legacy.foobar (name) VALUES ('abc')
testingdb-# ;
INSERT 17514 1
testingdb=# SELECT * FROM legacy.foobar ;
 foobar_id | name 
-----------+------
       106 | abc
(1 row)
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;My patch wouldn&amp;#8217;t figure that out because the sequence was not created by &lt;code&gt;SERIAL&lt;/code&gt;. So, my patch started to feel lame and a total waste of time, because I thought that it was fixing a problem.. that works pretty much as effectively as assuming it is &lt;code&gt;_seq&lt;/code&gt;.. but without needing to run a &lt;span class="caps"&gt;SQL&lt;/span&gt; query to determine that. We all (should) know that the field will be named like that when working with &lt;code&gt;SERIAL&lt;/code&gt;. So, my patch didn&amp;#8217;t buy us anything.&lt;/p&gt;


	&lt;p&gt;However, Active Record still doesn&amp;#8217;t support those funky sequence names. So, I found &lt;a href="http://dev.rubyonrails.com/ticket/1273"&gt;this ticket #1273&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Their approach was very similar to what caused me to use my long &lt;span class="caps"&gt;SQL&lt;/span&gt; query in the first place because this was suggested to me well over a year ago and I found it to not work in the following situation.&lt;/p&gt;


	&lt;p&gt;If I have two seperate schemas with the same table name in each like so:&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
=# \d legacy.people
                                       Table "legacy.people" 
  Column   |         Type          |                           Modifiers                           
-----------+-----------------------+---------------------------------------------------------------
 people_id | integer               | not null default nextval('legacy.people_people_id_seq'::text)
 name      | character varying(50) | 
Indexes:
    "people_pkey" PRIMARY KEY, btree (people_id)

=# \d foo.people
                                       Table "foo.people" 
  Column   |         Type          |                         Modifiers                          
-----------+-----------------------+------------------------------------------------------------
 people_id | integer               | not null default nextval('foo.people_people_id_seq'::text)
 name      | character varying(50) | 
Indexes:
    "people_pkey" PRIMARY KEY, btree (people_id)      
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;That patch will not work because you can&amp;#8217;t call the following query:&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
# SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'people');
ERROR:  more than one row returned by a subquery used as an expression  

&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;... because there are two tables with the same name! (fun, huh?)&lt;/p&gt;


	&lt;p&gt;Mine would work&amp;#8230; but why bother with that huge query? So, I took my ticket out of [PATCH] and decided that I thought it would be best to just assume that sequences are generated with &lt;code&gt;SERIAL&lt;/code&gt; ( &lt;a href="http://rubyurl.com/2W6"&gt;link&lt;/a&gt; ) by default in AR.&lt;/p&gt;


	&lt;p&gt;Okay, so what can we do about custom &lt;code&gt;SEQUENCE&lt;/code&gt; names?&lt;/p&gt;


	&lt;p&gt;Well, I am proposing the following (and mentioned this in the &lt;a href="http://dev.rubyonrails.com/ticket/2016"&gt;ticket #2016&lt;/a&gt;)...&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
class LegacyTable &amp;lt; ActiveRecord::Base
  def self.table_name() "legacy.foobar" end

  # new option for this
  set_primary_key "foobar_id", :sequence =&amp;gt; "legacy.old_sequence_name" 
end
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;(or something along those lines)&lt;/p&gt;


	&lt;p&gt;With this, I can work around these legacy database scenarios with a quick option.  Thoughts/opinions?&lt;/p&gt;


	&lt;p&gt;I decided to post this on my blog as well, because I do know that there are a few skeptical PostgreSQL people out there who read my blog&amp;#8230; I want you to know that I am looking out for you. ;-)&lt;/p&gt;


	&lt;p&gt;I am sick and tired.. and going to sleep now.&lt;/p&gt;
</description>
      <pubDate>Sat, 20 Aug 2005 02:05:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:4a72cbadfe138b7cbdf2195ab44d109c</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/08/20/postgresql-sequences-in-rails</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>PostgreSQL</category>
      <category>activerecord</category>
      <category>postgresql</category>
    </item>
    <item>
      <title>Are you a console master?</title>
      <description>&lt;p&gt;I have a few questions.&lt;/p&gt;


	&lt;p&gt;1.) Do you know what &lt;code&gt;./script/console&lt;/code&gt; does?&lt;/p&gt;


	&lt;p&gt;2.) If not, why not?&lt;/p&gt;


	&lt;p&gt;3.) If so, do you have any fun tips and tricks to share with the masses?&lt;/p&gt;


	&lt;p&gt;It occured to me earlier that many people, who might have came from the &lt;span class="caps"&gt;PHP&lt;/span&gt; camp, may have never really tested their object-oriented code from some sort of interactive program. (irb) If you are coming from the Python, Java, etc worlds, interactive testing isn&amp;#8217;t anything new. Rails is nice enough to bundle a console script right within it!&lt;/p&gt;


	&lt;p&gt;I meet people online who have never even tried to run it. There are not many tutorials on the wiki that show console&amp;#8230; and in my opinion, its one of the coolest things about Ruby and Rails. (but, I come from the php world&amp;#8230;)&lt;/p&gt;


	&lt;p&gt;So, if you aren&amp;#8217;t using it&amp;#8230; why not? got a moment? try this from the root path of your Rails application.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
./script/console
&lt;/pre&gt;
&lt;/code&gt;

It start up okay? If so, what is the name of one of your models? Let&amp;#8217;s say that I have a model structure like:
&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;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Order&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;belongs_to&lt;/span&gt; &lt;span class="symbol"&gt;:customer&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

From &lt;code&gt;console&lt;/code&gt;, you can access your models and do all sorts of fun things.
&lt;code&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; y = Customer.find(16)
=&amp;gt; #&amp;lt;Customer:0x2743ea4 @attributes={"name"=&amp;gt;"Robby", "id"=&amp;gt;"16"}&amp;gt;
&amp;gt;&amp;gt; y.orders
=&amp;gt; [#&amp;lt;Order:0x27416b8 @attributes={"id"=&amp;gt;"18", "amount"=&amp;gt;"12.00", "customer_id"=&amp;gt;"16"}&amp;gt;, #&amp;lt;Order:0x274167c @attributes={"id"=&amp;gt;"19", "amount"=&amp;gt;"12.50", "customer_id"=&amp;gt;"16"}&amp;gt;] 
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;Pretty neat, huh?&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
&amp;gt;&amp;gt; o = Order.find(18)
=&amp;gt; #&amp;lt;Order:0x273da68 @attributes={"id"=&amp;gt;"18", "amount"=&amp;gt;"12.00", "customer_id"=&amp;gt;"16"}&amp;gt;
&amp;gt;&amp;gt; o.customer.name
=&amp;gt; "Robby" 
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;If you are  remotely a console wizard, please share some tips and tricks for those who are not sure what to do with it. I personally find myself in console all the time that I am working with Rails, testing stuff out with my models, before I move any of the code to my application.&lt;/p&gt;


	&lt;p&gt;It sure beats, hitting refresh in your browser all day. :-)&lt;/p&gt;
</description>
      <pubDate>Thu, 18 Aug 2005 22:06:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:f15997a9e5983273a9269fd0bf666eeb</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>rails</category>
      <category>console</category>
      <category>activerecord</category>
    </item>
    <item>
      <title>Active Record, I &amp;lt;3 U but I still trust my database server (a tiny bit more)</title>
      <description>&lt;p&gt;While working on a portion of my book, I found myself in &lt;code&gt;./script/console&lt;/code&gt; and was seeing some weird issues when I would use &lt;code&gt;has_many&lt;/code&gt; and &lt;code&gt;belongs_to&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s take two simple models.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;pre&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Order&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;belongs_to&lt;/span&gt; &lt;span class="symbol"&gt;:customer&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&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="keyword"&gt;end&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="regex"&gt;pre&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="constant"&gt;After&lt;/span&gt; &lt;span class="ident"&gt;a&lt;/span&gt; &lt;span class="ident"&gt;few&lt;/span&gt; &lt;span class="ident"&gt;test&lt;/span&gt; &lt;span class="ident"&gt;records&lt;/span&gt;&lt;span class="punct"&gt;...&lt;/span&gt;

&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;pre&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="ident"&gt;test_dev&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="comment"&gt;# SELECT * FROM customers;  &lt;/span&gt;
 &lt;span class="ident"&gt;id&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;      &lt;span class="ident"&gt;name&lt;/span&gt;      
&lt;span class="punct"&gt;----+----------------&lt;/span&gt;
  &lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;Robby&lt;/span&gt;
  &lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;Nigel&lt;/span&gt;
  &lt;span class="number"&gt;3&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="constant"&gt;Linus&lt;/span&gt;
&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;3&lt;/span&gt; &lt;span class="ident"&gt;rows&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;

&lt;span class="ident"&gt;test_dev&lt;/span&gt;&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="comment"&gt;# SELECT * FROM orders;&lt;/span&gt;
 &lt;span class="ident"&gt;id&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;customer_id&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;amount&lt;/span&gt; 
&lt;span class="punct"&gt;----+-------------+--------&lt;/span&gt;
  &lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;           &lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;  &lt;span class="number"&gt;12.00&lt;/span&gt;
  &lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;           &lt;span class="number"&gt;3&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;  &lt;span class="number"&gt;12.00&lt;/span&gt;
&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="ident"&gt;rows&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="punct"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="regex"&gt;pre&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span class="punct"&gt;/&lt;/span&gt;&lt;span class="ident"&gt;code&lt;/span&gt;&lt;span class="punct"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="constant"&gt;Nothing&lt;/span&gt; &lt;span class="ident"&gt;completely&lt;/span&gt; &lt;span class="ident"&gt;crazy&lt;/span&gt; &lt;span class="ident"&gt;going&lt;/span&gt; &lt;span class="ident"&gt;on&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;right?&lt;/span&gt;

&lt;span class="punct"&gt;&amp;lt;&lt;/span&gt;&lt;span class="ident"&gt;typo&lt;/span&gt;&lt;span class="symbol"&gt;:code&lt;/span&gt; &lt;span class="ident"&gt;lang&lt;/span&gt;&lt;span class="punct"&gt;=&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;ruby&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class="constant"&gt;Loading&lt;/span&gt; &lt;span class="ident"&gt;development&lt;/span&gt; &lt;span class="ident"&gt;environment&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="ident"&gt;Customer&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;destroy&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;span class="punct"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="punct"&gt;{&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;name&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;=&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;Linus&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;id&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;=&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;3&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;}&lt;/span&gt;
&lt;span class="punct"&gt;&amp;gt;&amp;gt;&lt;/span&gt;     

&lt;span class="punct"&gt;=&lt;/span&gt;&lt;span class="comment"&gt;# SELECT * FROM orders;&lt;/span&gt;
 &lt;span class="ident"&gt;id&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;customer_id&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt; &lt;span class="ident"&gt;amount&lt;/span&gt; 
&lt;span class="punct"&gt;----+-------------+--------&lt;/span&gt;
  &lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;           &lt;span class="number"&gt;1&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;  &lt;span class="number"&gt;12.00&lt;/span&gt;
  &lt;span class="number"&gt;3&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;           &lt;span class="number"&gt;3&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;  &lt;span class="number"&gt;12.00&lt;/span&gt;
&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;2&lt;/span&gt; &lt;span class="ident"&gt;rows&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Wait a minute!  I just deleted a customer with an &lt;code&gt;id&lt;/code&gt; of &lt;code&gt;3&lt;/code&gt;!&lt;/p&gt;


	&lt;p&gt;So, what is wrong with this scenario? Can you think of any potential problems that could occur from data like this? The record has a &lt;code&gt;customer_id&lt;/code&gt; for a customer that does not exist. This is why we have relational databases in the first place, right? :-)&lt;/p&gt;


	&lt;p&gt;Here is something that I learned today that I was unaware of. Active Record allows you to pass the &lt;code&gt;has_method&lt;/code&gt; declaration the option &lt;code&gt;:dependent&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;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;What is this option? Well, according to the AR documentation, &amp;#8220;&lt;code&gt;:dependent&lt;/code&gt; &amp;#8211; if set to true all the associated object are destroyed alongside this object. May not be set if &lt;code&gt;:exclusively_dependent&lt;/code&gt; is also set.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;In a nutshell, this works like &lt;code&gt;ON DELETE CASCADE&lt;/code&gt; does in PostgreSQL. So, it will through and delete the &lt;em&gt;orders&lt;/em&gt; associated with the &lt;em&gt;customer&lt;/em&gt; that I was attempting to &lt;code&gt;destroy&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Up until today, I hadn&amp;#8217;t broken myself out of the habit of using the built-in constraints/triggers of PostgreSQL. So, as soon as I did, this issue came up and I learned about &lt;code&gt;:dependent&lt;/code&gt;.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
test_dev# \d orders
                                Table "public.orders" 
   Column    |     Type      |                       Modifiers                        
-------------+---------------+--------------------------------------------------------
 id          | integer       | not null default nextval('public.orders_id_seq'::text)
 customer_id | integer       | 
 amount      | numeric(10,2) | 
Indexes:
    "orders_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "orders_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(id)

test_dev=# ALTER TABLE orders DROP CONSTRAINT orders_customer_id_fkey;
ALTER TABLE 

RobbyOnRails:~/Programming/footest robbyrussell$ ./script/console 
Loading development environment.
&amp;gt;&amp;gt; cust = Customer.create(:name =&amp;gt; 'Jim')
=&amp;gt; #&amp;lt;Customer:0x275373c @new_record_before_save=true, @new_record=false, @attributes={"name"=&amp;gt;"Jim", "id"=&amp;gt;5}, @errors=#&amp;lt;ActiveRecord::Errors:0x274fa88 @base=#&amp;lt;Customer:0x275373c ...&amp;gt;, @errors={}&amp;gt;&amp;gt;
&amp;gt;&amp;gt; cust.orders.create(:amount =&amp;gt; '25.00')
=&amp;gt; #&amp;lt;Order:0x274991c @new_record=false, @attributes={"id"=&amp;gt;4, "amount"=&amp;gt;"25.00", "customer_id"=&amp;gt;5}, @errors=#&amp;lt;ActiveRecord::Errors:0x2746dfc @base=#&amp;lt;Order:0x274991c ...&amp;gt;, @errors={}&amp;gt;&amp;gt;
&amp;gt;&amp;gt;                

test_dev=# SELECT * FROM orders;
 id | customer_id | amount 
----+-------------+--------
  1 |           1 |  12.00
  3 |           4 |  29.00
  4 |           5 |  25.00
(3 rows)
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;As you can see, I put myself into the hands of Active Record when I ran the &lt;code&gt;DROP CONSTRAINT&lt;/code&gt;. Then I tried running the code at the top of the post&amp;#8230; and it didn&amp;#8217;t work.&lt;/p&gt;


	&lt;p&gt;According to the docs, if you use &lt;code&gt;:dependent =&amp;gt; true&lt;/code&gt;, it should delete the foreign table records. If not, it should set the value of the foriegn key field to &lt;code&gt;NULL&lt;/code&gt; in the foreign table.&lt;/p&gt;


Basically, perform these two &lt;span class="caps"&gt;SQL&lt;/span&gt; queries:
&lt;code&gt;
&lt;pre&gt;
UPDATE orders SET customer_id = NULL WHERE customer_id = 17; 

DELETE FROM customers  WHERE id = 17;
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;Then, the records are still in the database for those orders, but the customer is deleted. There are arguments for and against doing this sort of thing&amp;#8230; but the ability to have the option is always nice. In any event, Active Record would not run the first query,it was just deleting from the customers table. Without my constraint, no error would be returned from PostgreSQL and I started to get some bad data.&lt;/p&gt;


	&lt;p&gt;Imagine showing a list of orders and trying to display the customer name associated with an order that has no linking customer. Doh! If Active Record sets the &lt;code&gt;customer_id&lt;/code&gt; to &lt;code&gt;NULL&lt;/code&gt; we can at least have some logic to work with this without having to run some fun &lt;span class="caps"&gt;SQL&lt;/span&gt; queries to figure out which orders do and dont have customers. (we want our applications to have clean data!)&lt;/p&gt;


	&lt;p&gt;Anyhow&amp;#8230; Was this a bug? Should Active Record know to update the records to &lt;code&gt;NULL&lt;/code&gt; in this case? I figured that is should be handling this task, especially since it was handling cascading deletes when you passed &lt;code&gt;:dependent =&amp;gt; true&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;However, I didn&amp;#8217;t want to prematurely post a bug report, so I began asking around on &lt;b&gt;#rubyonrails&lt;/b&gt; (irc.freenode.net). People made a bunch of suggestions as to how to work around it. I could add a &lt;code&gt;before_destroy&lt;/code&gt; method in my model, track the bug down, (re)add an &lt;code&gt;ON DELETE&lt;/code&gt; trigger to my table (hah), etc. So, I decided that I would see if I could track down what happens when has_many is used for a model upon &lt;code&gt;#destroy&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;After a while of digging and making some tests, I posted a &lt;a href="http://rubyurl.com/Lgv"&gt;patch&lt;/a&gt; and a &lt;a href="http://dev.rubyonrails.com/ticket/2009"&gt;bug report&lt;/a&gt;. (please disregard my first patch&amp;#8230; it did not work! heh)&lt;/p&gt;


	&lt;p&gt;Now that I figured this out, I am going to happy add my constraints back to my tables and go back to playing around. This reminded me of a post I had a few months ago when I mentioned that I thought it was best to put some constraints and logic in the database. I also agree that constraints should be put in the abstraction layer, but we cannot always put all faith in our code either. A few levels of checks doesn&amp;#8217;t hurt. :-)&lt;/p&gt;


	&lt;p&gt;This was a fun little riddle that I took on today. The moral of the story? If you have the ability to use the builtin referential integrity features of PostgreSQL and those &lt;strong&gt;other&lt;/strong&gt; databases, it might be a good idea to do so. Things get overlooked, people login to the database in many ways, and from different programs.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;: &lt;span class="caps"&gt;DHH&lt;/span&gt; responded to this post and provided &lt;a href="http://martinfowler.com/bliki/DatabaseStyles.html"&gt;a link&lt;/a&gt; which discusses &lt;a href="http://martinfowler.com/bliki/ApplicationDatabase.html"&gt;Application Database&lt;/a&gt; versus &lt;a href="http://martinfowler.com/bliki/IntegrationDatabase.html"&gt;Integrated Database&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;It should be noted that there is an important distinction between the two methods. When I said, &amp;#8220;Things get overlooked, people login to the database in many ways, and from different programs&amp;#8221; I was basically describing Integration Database. However, I was also thinking of the possibility of someone opening up their MySQL or PostgreSQL &lt;span class="caps"&gt;GUI&lt;/span&gt; and manually removing a record in plain &lt;span class="caps"&gt;SQL&lt;/span&gt;. According to Application Database, the moment that you do that, you basically break this model and cannot expect your application to be fully responsible for the problems that may or may not occur. At this point, you would need to look at your application in terms of Integration Database. Please do correct me if I am wrong on this. :-)&lt;/p&gt;


	&lt;p&gt;However, with this scenario, my first attempt to move to relying on AR had a minor hiccup, but it was an easy enough fix.&lt;/p&gt;


	&lt;p&gt;By performing the following command, I am moving towards an Integration Database pattern and that should be recognized when taking this into consideration.&lt;/p&gt;


&lt;code&gt;
&lt;pre&gt;
ALTER TABLE orders ADD CONSTRAINT orders_customers_id_fkey 
    FOREIGN KEY (customer_id) REFERENCES customers (id) MATCH FULL; 
&lt;/pre&gt;
&lt;/code&gt;

	&lt;p&gt;Okay, back to work!&lt;/p&gt;


	&lt;p&gt;Once again. Use constraints! (if you can)&lt;/p&gt;


	&lt;p&gt;... and thanks to &lt;a href="http://www.loudthinking.com/"&gt;&lt;span class="caps"&gt;DHH&lt;/span&gt;&lt;/a&gt; for providing the link and motivating me to make a note of this in my entry.&lt;/p&gt;
</description>
      <pubDate>Thu, 18 Aug 2005 21:20:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:025a8f69575ed0fcc3b30cce68042568</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2005/08/18/active-record-i-3-u-but-i-still-trust-my-database-server-a-tiny-bit-more</link>
      <category>Ruby on Rails</category>
      <category>Ruby</category>
      <category>Programming</category>
      <category>activerecord</category>
      <category>rails</category>
      <category>ruby</category>
      <category>postgresql</category>
    </item>
  </channel>
</rss>
