<?xml version="1.0" encoding="UTF-8"?>
<posts type="array">
  <post>
    <body>&lt;h4&gt;First, a little context&lt;/h4&gt;

&lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/ICalendar"&gt;`RFC 2445` (iCalendar)&lt;/a&gt; spec has become a pseudo standard for internet scheduling.  Google Calendars, Microsoft Outlook, and Apples iCal software all support rendering iCalendar feeds. &lt;a href="http://github.com/rubyredrick/ri_cal/tree/master"&gt;RiCal&lt;/a&gt; is Rubys latest (and most complete) implementation for both creating and parsing the format.&lt;/p&gt;

&lt;p&gt;So, why am I suddenly so interested in calendars?  I've been doing a lot of freelance work lately for an Ottawa based company named &lt;a href="http://www.dejatechnologies.com"&gt;Deja Technologies&lt;/a&gt;, and so happens, one of their clients needed a corporate calendaring system for their employees that could be integrated into the intranet site that I was building.&lt;/p&gt;

&lt;h4&gt;Google Calendar Woes&lt;/h4&gt;

&lt;p&gt;I've developed a bad habit of blindly choosing Google as my default solution provider.  Google Maps is fantastic, Docs is amazing, why should their Calendar be any different?  I had a seemingly simple idea: generate an iCalendar feed, and render it in in an embedded Google Calendar.  It worked perfectly, almost.  My software generated the iCalendar feed, Google Calendar displayed it correctly, and it embedded into the page easily. &lt;/p&gt;

&lt;p&gt;What was the deal breaker?  Caching.  At the time of posting, Google Calendar caches iCalendar feeds for anywhere between 3-6 hours.  This is not manually refreshable.  Such a limitation makes Google Calendars virtually useless as an embedded calendar solution.  Google Calendar is still amazing, but it wasn't quite the solution I was hoping for.&lt;/p&gt;

&lt;h4&gt;Won't Somebody Please, Render My Feed?&lt;/h4&gt;

&lt;p&gt;Maybe I was looking in the wrong places, but their aren't embedded many iCalendar rendering services on the web (note to self)!  I attempted to use &lt;a href="http://instantcal.com/"&gt;InstantCal&lt;/a&gt;, and everything worked fine, for awhile.  Unfortunatly, they seem to have a bug in their feed parsing.  It started doubling up reoccuring events after January 2010.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://phpicalendar.net/"&gt;PHP iCalendar&lt;/a&gt; &lt;i&gt;might&lt;/i&gt; have worked, but this was a Ruby project, and if I was going to start running my own calendar, I wanted a Ruby solution.&lt;/p&gt;

&lt;h4&gt;If You Want Something Done Right..&lt;/h4&gt;

&lt;p&gt;I decided that I had wasted enough time trying to &lt;i&gt;find&lt;/i&gt; a solution.  It would be much easier (and more fun) to just build one.  I decided to use the &lt;a href="http://github.com/rubyredrick/ri_cal/tree/master"&gt;ri_cal&lt;/a&gt; gem to parse my feed, and give me the events.  Once I knew the events, drawing the calendar would be a piece of cake.&lt;/p&gt;

&lt;p&gt;Install the require gems.&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
gem install ri_cal
&lt;/code&gt;
&lt;/p&gt;

&lt;h4&gt;Calendar &amp; Calendar Event Models&lt;/h4&gt;
&lt;p&gt;
&lt;code&gt;
class Calendar &lt; ActiveRecord::Base
        require 'ri_cal'

        has_many :calendar_events
        attr_accessor :ics

        def to_ics(reload = false)

                if !@ics || reload

                        icalendar = RiCal.Calendar do |c|
                                self.calendar_events.each { |calendar_event|

                                        c.event do |e|
                                                e.summary = calendar_event.title.to_s
                                                e.description = calendar_event.description.to_s
                                                if calendar_event.all_day || calendar_event.start.to_date &gt; calendar_event.end.to_date
                                                        e.dtstart = calendar_event.start.to_date
                                                        e.dtend = calendar_event.start.advance(:days =&gt; 1).to_date
                                                elsif calendar_event.all_day
                                                        e.dtstart = calendar_event.start.to_date
                                                        e.dtend = calendar_event.end.to_date
                                                else
                                                        e.dtstart =  calendar_event.start
                                                        e.dtend = calendar_event.end
                                                end
                                                e.location = calendar_event.location.to_s

                                                if !calendar_event.frequency.to_s.empty? &amp;&amp; CONFIG[:calendar]["frequency"].collect { |i| i[1] }.include?( calendar_event.frequency )
                                                        recurrence = []
                                                        recurrence &lt;&lt; "FREQ=#{calendar_event.frequency}" if calendar_event.frequency
                                                        recurrence &lt;&lt; "INTERVAL=#{calendar_event.interval}" if calendar_event.interval
                                                        recurrence &lt;&lt; "UNTIL=#{ calendar_event.until.strftime('%Y%m%dT%H%M%SZ') }" if calendar_event.until
                                                        e.rrule = recurrence.join(";") if recurrence.length &gt; 0
                                                end

                                        end

                                }

                        end
                        @ics = icalendar
                end

                @ics
        end

        def bounded_events(start_date, end_date)
                events = self.to_ics.events.collect { |i|
                        i.occurrences(:starting =&gt; start_date, :before =&gt; end_date).collect { |j|
                                { :summary =&gt; j.summary,
                                  :description =&gt; j.description,
                                  :location =&gt; j.location,
                                  :start =&gt; j.dtstart,
                                  :end =&gt; j.dtend }
                        }
                }
                events.reject { |i| i.empty? }.flatten
        end

        def bounded_events_by_date(start_date,end_date)
                events = self.bounded_events(start_date, end_date)
                rtn_hash = {}
                events.each { |e|
                        rtn_hash[e[:start].strftime("%Y-%m-%d")] ||= []
                        rtn_hash[e[:start].strftime("%Y-%m-%d")] &lt;&lt; e
                }

                rtn_hash.each_pair { |k,v|

                        v.sort! { |a,b| a[:summary]&lt;=&gt;b[:summary] }.sort! { |a,b| a[:start]&lt;=&gt;b[:start] }
                }

                rtn_hash
        end

end

class CalendarEvent &lt; ActiveRecord::Base
        require 'ri_cal'
        belongs_to :calendar

        validates_presence_of :title
        validates_presence_of :start
        validates_presence_of :end

end


&lt;/code&gt;
&lt;/p&gt;

&lt;h4&gt;Database Migrations&lt;/h4&gt;
&lt;p&gt;
&lt;code&gt;
class CreateCalendars &lt; ActiveRecord::Migration
  def self.up
    create_table :calendars do |t|
                t.column :name, :string
      t.timestamps
    end
  end

  def self.down
    drop_table :calendars
  end
end

class CreateCalendarEvents &lt; ActiveRecord::Migration
  def self.up
    create_table :calendar_events do |t|
                t.column :calendar_id, :integer
                t.column :start, :datetime
                t.column :end, :datetime

                t.column :frequency, :string
                t.column :interval, :integer
                t.column :until, :datetime

                t.column :description, :string
                t.column :location, :string

                t.column :all_day, :boolean
      t.timestamps
    end
  end

  def self.down
    drop_table :calendar_events
  end
end
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;Stay tuned for part 2, where I'll go over how to turn all of this into a calendar.&lt;/p&gt;
</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-08-21T18:48:26Z</created-at>
    <description>Calendar feed tutorial in Ruby on Rails</description>
    <id type="integer">16</id>
    <keywords>iCalendar, ri_cal, ruby on rails</keywords>
    <sub-title>Build your own calendar with reoccurring events using ri_cal </sub-title>
    <title>Event Calendars in Rails</title>
    <updated-at type="datetime">2009-08-21T18:56:50Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;Running a Rails application under Windows server is a far from the ideal deployment environment, but in some instances it must be done.
In my case, a Windows 2003 box is the only server available to my employer.  It was deployed before I arrived in my current position,
and despite my best efforts and arguments to management, still remains.&lt;/p&gt;

&lt;p&gt;The server is currently running 3 Rails applications that I've developed, each application with 4 mongrel instances running as services.  
Apache is used in the front end, and as far as I was aware, everything worked smoothly.&lt;/p&gt;

&lt;p&gt;One day I arrived at work very early, 6am to be exact, and for some reason, received a 503 proxy error when checking one of the applications!  
Every application gave the same 503 Proxy error.  Mongrel seemed to be going to sleep after several hours of inactivity. 
Google did not have any answers, either.&lt;/p&gt;

&lt;h4&gt;The Fix&lt;/h4&gt;

&lt;p&gt;Luckily the solution to this problem is simple enough, I would just set up a Windows "Scheduled Task" (cron job to all you Linux folk) to hit my mongrel instances every half hour.  I haven't had the problem since.  
There is even an added benefit - the applications seem to respond even more quickly, probably due to my constant prodding at Mongrel.&lt;/p&gt;

&lt;p&gt;The implementation is very hack-ish.  My original thought was just to use Windows Telnet to make the requests, but that turned out to be much harder than I thought.  
The Windows version of Telnet does not seem to like being handled by a batch file.  
After 20 minutes, I gave up on Windows Telnet.  Oh, it was easy enough to manually request the page from the server using Telnet, but for the life of me I could not get the batch file to do it.  
I'm too used to the Linux shell I suppose&lt;/p&gt;

&lt;p&gt;Next, I tried PuTTY.  Again, it was very easy to get it to work under the actual PuTTY client.  But, getting Plink (PuTTY's command-line only version) to accomplish the same feat through a batch file was like pulling teeth.&lt;/p&gt;

&lt;p&gt;For everyone not keeping score:&lt;/p&gt;

&lt;table&gt;
&lt;tr&gt;
  &lt;th&gt;Windows&lt;/th&gt;&lt;td&gt; 2&lt;/td&gt;
foreach($sites as $site) {

	$file = fopen( $site,"r");
	if (!$file) {
		// Error opening site
	} else {
		// Site opened
	}
	fclose($file);
}
&lt;/tr&gt;
&lt;tr&gt;
  &lt;th&gt;Me&lt;/th&gt;&lt;td&gt; 0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;br/&gt;
&lt;p&gt;At this point, frustration and desperation kicked in.  I turned to the quickest solution I could think of: Ruby!&lt;/p&gt;

&lt;h4&gt;mongrel_keepalive.bat&lt;/h4&gt;
&lt;p&gt;
&lt;code&gt;
@echo off
ruby &lt; mongrel_keepalive.rb
&lt;/code&gt;
&lt;/p&gt;

&lt;h4&gt;mongrel_keepalive.rb&lt;/h4&gt;
&lt;p&gt;
&lt;code&gt;
require 'net/http'

sites = [
	"http://127.0.0.1:5000/", 
	"http://127.0.0.1:5001/", 
	"http://127.0.0.1:5002/", 
	"http://127.0.0.1:5003/", 
	
	"http://127.0.0.1:5100/",
	"http://127.0.0.1:5101/",
	"http://127.0.0.1:5102/",
	"http://127.0.0.1:5103/",

	"http://127.0.0.1:5200/",
	"http://127.0.0.1:5201/",
	"http://127.0.0.1:5202/",
	"http://127.0.0.1:5203/"
]

sites.each { |i|
	Net::HTTP.get( URI.parse( i ) )
}

&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-07-04T20:46:44Z</created-at>
    <description></description>
    <id type="integer">15</id>
    <keywords>ruby on rails, ruby, rails, mongrel, windows, apache, proxy error</keywords>
    <sub-title>Windows, Mongrel, and an Apache proxy error!</sub-title>
    <title>Mongrel Falls Asleep</title>
    <updated-at type="datetime">2009-07-05T18:24:17Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;Today we're going to solve the problem of implementing searching and sorting in an index view.  By providing your users with an easy method of sorting and searching though their views you are increasing their happiness and productivity, and by implementing searching and sorting in an easy to understand and maintainable manner you are ensuring your own sanity.&lt;/p&gt;

&lt;p&gt;All of the utilities required to accomplish searching and sorting your data is already provided to you in your SQL database.  The real trick is to get queries to the database that are both injection safe and are generated in a manner that is easy for you or your eventual replacements to understand.&lt;/p&gt;

&lt;p&gt;It's very tempting to rush ahead and implement some fancy AJAX features right away, but I urge you to stop and think about first implementing legacy support, and than dealing with more advanced features.  It's always necessary to ensure graceful degradation in all the applications that you write.&lt;/p&gt;

&lt;h4&gt;Fat Models, Skinny Controllers&lt;/h4&gt;

&lt;p&gt;Implementing business logic in your controllers is one of the most tempting and messy practices that you can fall into the habit of.  Just `getting it working` and than `cleaning it up later` is not a good way to write code, and I urge everyone to always design twice, and code once.&lt;/p&gt;

&lt;p&gt;Our user will have to pass to the Controller how they would like the data searched and sorted, the controller will ask the appropriate Model to carry out the work, and the view will display the result.  The real trick is to figure out how the Model should implement and return the different scopes that the data can be ordered in, and how should we indicate to the view how that data should be displayed.&lt;/p&gt;

&lt;h4&gt;Project Setup&lt;/h4&gt;

&lt;p&gt;A project can have many programmers, but only one manager.  The following models will be assumed throughout the all of the examples.  &lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class Person &lt; ActiveRecord::Base 
end 

class ProjectProgrammer &lt; ActiveRecord::Base 
	belongs_to :project
	belongs_to :person
end

class Project &lt; ActiveRecord::Base 
	belongs_to :manager, :foreign_key =&gt; :manager_id, :class_name =&gt; "Person"
	has_many :project_programmers
	has_many :programmers, :through =&gt; :project_programmers, :source =&gt; :person, :class_name =&gt; "Person"
end
&lt;/code&gt;
&lt;p&gt;

&lt;h4&gt;Named Scopes&lt;/h4&gt;

&lt;p&gt;Named scopes allow for some pretty amazing things, and will be the underpinning of our sorting and searching.  There are many places on the Internet that &lt;a href='http://ryandaigle.com/articles/2008/3/24/what-s-new-in-edge-rails-has-finder-functionality'&gt;better explain exactly what named scopes do&lt;/a&gt;.  I am going to explain how to used named scopes to quickly and cleanly sort and search your data.&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class Project &lt; ActiveRecord::Base 
	#..
	Scopes = { 
		:manager_asc =&gt; { 
			:order =&gt; "manager.`last_name` ASC, manager.`first_name` ASC", 
			:joins =&gt; "LEFT JOIN people AS manager ON manager.`id` = projects.`manager_id`" 
			} 
		}, 
		:manager_desc =&gt; { 
			:order =&gt; "manager.`last_name` DESC, manager.`first_name` DESC", 
			:joins =&gt; "LEFT JOIN people AS manager ON manager.`id` = projects.`manager_id`" 
			} 
		}
	} 
	named_scope :by_scope, lambda { |scope| 
		return {} unless Project::Scopes[scope.to_sym]; 
		Project::Scopes[scope.to_sym] 
	}
	#..
end 
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;Load up the console:&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
&gt;&gt; Project.by_scope('manager_asc')
&gt;&gt; Project.by_scope('manager_desc')
&lt;/code&gt;
&lt;/p&gt;

&lt;p&gt;This is a basic example, but you get the idea.  You can define any number of pre-canned views in your Model, and call them from your views automatically by passing the appropriate scope.  Injection safe, easy to understand!&lt;/p&gt;

&lt;p&gt;I'll leave it up to you as to how you want to implement searching, but from the above example I'm sure I've already given you a few ideas.&lt;/p&gt;

&lt;p&gt;Small disclaimer:  The above code hasn't actually been tested, and was done from memory, so you may or may not have syntax errors.  The essence of the method is there, however.&lt;/p&gt;

&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-06-02T20:58:48Z</created-at>
    <description></description>
    <id type="integer">10</id>
    <keywords>ruby on rails, ruby, rails, named scopes, custom ordering, tutorial</keywords>
    <sub-title></sub-title>
    <title>Custom Ordering Using Named Scopes</title>
    <updated-at type="datetime">2009-07-04T21:39:12Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;I ran into an interesting problem today, all of my RJS calls refused to execute!  Of course, In the end, it was entirely my fault.  I had completed the AJAX DOM manipulation code, tested it for any mistakes, and had forgotten about it while I attended to other parts of the application.&lt;/p&gt;
&lt;p&gt;When deploying for some internal testing, I noticed that the DOM wasn't updating.  Very strange, especially since there were no errors reported.&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;Firebug reported that my request had completed.&lt;/li&gt;
	&lt;li&gt;A `puts "test"` verfied that code was executed before, and after the offending RJS file.&lt;/li&gt;
	&lt;li&gt;Everything was OK with the response.&lt;br/&gt;&lt;br/&gt;
		&lt;code&gt;
try { 
	alert("the response seems to be evaled fine!"); 
} catch (e) {  
	alert('RJS error:\n\n' + e.toString()); alert('alert(\"the response seems to be evaled fine!\");'); throw e  
}
		&lt;/code&gt;
	&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I was stumped, and thus began the arduous process of determining what I had done.  After some code tracing, I stumbled upon this block in prototype.js:&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
respondToReadyState: function(readyState) { 
  
	var state = Ajax.Request.Events[readyState], response = new Ajax.Response(this); 

	if (state == 'Complete') { 

		try {
			this._complete = true;
		(this.options['on' + response.status] || this.options['on' + (this.success() ? 'Success' : 'Failure')] || Prototype.emptyFunction)(response, response.headerJSON);
		} catch (e) {
			this.dispatchException(e);
		}

	var contentType = response.getHeader('Content-type');

	if (this.options.evalJS == 'force' || (this.options.evalJS &amp;&amp; this.isSameOrigin() &amp;&amp; contentType &amp;&amp; contentType.match(/^\s*(text|application)\/(x-)?(java|ecma)script(;.*)?\s*$/i))) {
		this.evalResponse();
	}
}
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;The console reported code executing before, and after this.evalResponse, but not inbetween!  What had I done to break things?  It all came down to my response headers.&lt;/p&gt;
&lt;p&gt;The data for this project was migrated from a legacy MS Access program, so all data was in the Western ISO-8859-1 character set.  Instead of fixing the data, I lazily forced all headers to "text/html; charset=ISO-8859-1".&lt;/p&gt;
&lt;p&gt;This had the side effect of also forcing my RJS response headers to "text/html; charset=ISO-8859-1", while prototype is expecting "text/javascript;".&lt;/p&gt;
&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-05-28T18:17:06Z</created-at>
    <description></description>
    <id type="integer">8</id>
    <keywords>ruby on rails, ruby, rails, ajax, response, tutorial</keywords>
    <sub-title>check your response headers</sub-title>
    <title>Rails AJAX Response Refuses to Execute</title>
    <updated-at type="datetime">2009-07-04T21:38:48Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;Today I've decided to take my best shot at explaining the Ruby on Rails multi-model form.  This is an important skill for any Rails enthusiast to have a good grasp on, and is one that personally took me a while to get.  The technique I'll be using is described very well in the &lt;a href='http://www.railscasts.com'&gt;Railscasts&lt;/a&gt; &lt;a href='http://railscasts.com/episodes/75-complex-forms-part-3'&gt;complex forms&lt;/a&gt; episode, and is another excellent resource for learning this technique.  &lt;/p&gt;
&lt;p&gt;Why is this an extremely important technique?&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;It allows for easy and understandable multi-form validations.&lt;/li&gt;
	&lt;li&gt;It gives us skinny controllers and fat models.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;Project Setup&lt;/h4&gt;
&lt;p&gt;Define a Train model, which will be comprised of many Cars. Our join model will be TrainCar.  To do this from the terminal, type:&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
&gt;&gt; ruby ./script/generate scaffold Train 
&gt;&gt; ruby ./script/generate model Car 
&gt;&gt; ruby ./script/generate model TrainCar 
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;This example is good because any Train can be comprised of any number of cars at any given time. Also notice the `position` column on the train_cars table.  This will let us order the cars in our train.&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class Train &lt; ActiveRecord::Base &lt;br/&gt;
	has_many :train_cars 
	has_many :cars, :through =&gt; :train_cars, :order =&gt; "train_cars.`position` ASC" 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class Car &lt; ActiveRecord::Base 
	has_many :train_cars 
	has_many :trains, :through =&gt; :train_cars, :order =&gt; "train_cars.`position` ASC" 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class TrainCar &lt; ActiveRecord::Base 
	belongs_to :train 
	belongs_to :car 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;In the migrations: &lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
create_table :trains do |t| 
	t.column :name, :string 
	t.timestamps 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
create_table :cars do |t| 
	t.column :name, :string 
	t.timestamps 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
create_table :train_cars do |t| 
	t.column :car_id, :integer 
	t.column :train_id, :integer
	t.column :position, :integer 
	t.timestamps 
end
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;Migrate your database:&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
	&gt;&gt; rake db:migrate &lt;br/&gt;
	&lt;/code&gt;
&lt;/p&gt;
&lt;h4&gt;Define Attribute Accessors&lt;/h4&gt;
&lt;p&gt;Your previous attempts at multi-model forms may have included attempting to build your multi-model associations from the passed hash within the controller.  We're going to DRY up that method a significant amount by off loading all of the model association building to where they originally belonged: the model.&lt;/p&gt;
&lt;p&gt;Open up your train model, and add the following code: &lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
class Train &lt; ActiveRecord::Base 
	has_many :train_cars 
	has_many :cars, :through =&gt; :train_cars, :order =&gt; "train_cars.`position` ASC" 

	after_update :save_train_cars 

	def new_train_car_attributes=(train_car_attributes) 
		train_car_attributes.each_pair do |object_id,attributes| 
			train_cars.build(attributes) 
		end 
	end 
  
	def existing_train_car_attributes=(train_car_attributes) 
		train_cars.reject(&amp;:new_record?).each do |train_car| 
			attributes = train_car_attributes[train_car.id.to_s] 
			if attributes 
				train_car.attributes = attributes 
			else 
				train_cars.delete(train_car) 
			end 
		end 
	end 
  
	def save_train_cars 
		train_cars.each do |train_car| 
			train_car.save(false) 
		end 
	end 
end 
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;This does exactly what you might think.  Now, your train model can handle the attributes passed from our controller hash params[:train][:new_train_car_attributes] and params[:train][:existing_train_car_attributes].  Additionally, after the update completes, those changes are saved to the database through the `save_train_cars` method.&lt;/p&gt;

&lt;h4&gt;Define our Helper Method&lt;/h4&gt;
&lt;p&gt;The helper method we're going to define here is going to be used to get data into those `new_train_car_attributes` and `existing_train_car_attributes` methods that we just defined in the Train model.&lt;/p&gt;
&lt;p&gt;Open up app/helpers/trains_helper.rb&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
module TrainsHelper
		def fields_for_train_car(train_car, &amp;block)

			if train_car.new_record?
				train_car_id = ((train_car.object_id &gt; 0)? train_car.object_id * -1 : train_car.object_id)
				fields_for("train[new_train_car_attributes][#{train_car_id}]", train_car, &amp;block)
			else
				fields_for("train[existing_train_car_attributes][]", train_car, &amp;block)
			end

		end
end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;If you pass the `fields_for_train_car` helper a TrainCar object, and it will determine whether it is a new or existing TrainCar (saved in the database or not), and put all of its attributes in the appropriate hash (`new_train_car_attributes` or `existing_train_car_attributes`) to be processed by the Train model.&lt;/p&gt;

&lt;h4&gt;The Train Controller&lt;/h4&gt;
&lt;p&gt;Open the controller that was automatically generated by the scaffold at app/controllers/trains_controller.rb.&lt;/p&gt;
&lt;p&gt;We're going to leave this file virtually unchanged, accept for the addition of a single line to the `update` method.&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
def update
	@train = Train.find(params[:id])
	params[:train][:existing_train_car_attributes] ||= {}

	respond_to do |format|
		if @train.update_attributes(params[:train])
			flash[:notice] = 'Train was successfully updated.'
			format.html { redirect_to(@train) }
			format.xml  { head :ok }
		else
			format.html { render :action =&gt; "edit" }
			format.xml  { render :xml =&gt; @train.errors, :status =&gt; :unprocessable_entity }
		end
	end
end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;Adding params[:train][:existing_train_car_attributes] ||= {} means that if there is no container `existing_train_car_attributes` in the `train` hash, create an empty one.  This is needed to trigger the `existing_train_car_attributes` method within the Train model if all cars have been removed from the train.  Since the hash contains no elements, it would otherwise not fire, and not remove all of the cars from the train as was intended.&lt;/p&gt;

&lt;h4&gt;The Edit View&lt;/h4&gt;
&lt;p&gt;The final piece of our puzzle will be the actual view that we'll be using to editing the multi-model form.  We will be using an AJAX approach for adding and removing elements, so don't forget to include Prototype/Scriptaculous to your layout file.&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;In edit.html.erb and new.html.erb add:&lt;br/&gt; &lt;br/&gt;
		&lt;code&gt;
&amp;lt;% form_for(@train) do |f| %&amp;gt;&lt;br/&gt;
	...&lt;br/&gt;
	&amp;lt;%= render :partial =&gt; 'form', :locals =&gt; { :f =&gt; f, :train =&gt; @train} %&amp;gt;&lt;br/&gt;
	...&lt;br/&gt;
&amp;lt;% end %&amp;gt;
		&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
		This will take care of both the 'new' and 'edit' views with our _form.html.erb partial.&lt;br/&gt;&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;Create the file app/views/trains/_form.html.erb, add:&lt;br/&gt;&lt;br/&gt;
		&lt;code&gt;
&amp;lt;% content_tag("ul") do %&amp;gt;
	&amp;lt;%= content_tag("dl", f.label(:name) ) %&amp;gt;
	&amp;lt;%= content_tag("dd", f.text_field(:name) ) %&amp;gt;
&amp;lt;% end %&amp;gt;

&amp;lt;% content_tag("fieldset") do %&amp;gt;
	&amp;lt;% content_tag("legend") do %&amp;gt;
	Cars
	&amp;lt;%= select_tag("car_id", options_for_select( Car.find(:all).collect { |i| [i.name,i.id] } ), { :id =&amp;gt; "car_id" }  ) %&amp;gt;
	&amp;lt;%= link_to_function("add", 
											
remote_function( :url =&amp;gt; { :controller =&amp;gt; :trains, :action =&amp;gt; :add_train_car, :train_id =&amp;gt; train.id }, 
											
:with =&amp;gt; "'car_id='+$(\"car_id\").options[ $(\"car_id\").options.selectedIndex ].value" ) ) %&amp;gt;
	&amp;lt;% end %&amp;gt;

	&amp;lt;% content_tag("div", { :id =&amp;gt; "train_cars" }) do %&amp;gt;
	&amp;lt;% @train.train_cars.each { |train_car| %&amp;gt;
		&amp;lt;%= render :partial =&amp;gt; 'train_car', :locals =&amp;gt; { :train_car =&amp;gt; train_car } %&amp;gt;
	&amp;lt;% } %&amp;gt;
	&amp;lt;% end %&amp;gt;

&amp;lt;% end %&amp;gt;
		&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
		This is the actual partial.  All of our train cars will go into the DIV#train_cars container.   The select_tag contains all possible trains for us to add.  The add button summons our add_train_car.rjs file to plunk the trains down into our DIV#train_cars container.  Comprende?&lt;br/&gt;&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;Create the file app/views/trains/add_train_car.rjs, add: &lt;br/&gt;&lt;br/&gt;
		&lt;code&gt;
train_car = TrainCar.new(:train_id =&gt; params[:train_id], :car_id =&gt; params[:car_id])
if train_car.valid?
	page.insert_html :bottom, :train_cars,:partial =&gt; 'train_car', :locals =&gt; {:train_car =&gt; train_car}
end
		&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
		The add_train_car RJS file will link up the train and the car, and place it at the bottom of DIV#train_cars.  Please note that &lt;b&gt;nothing&lt;/b&gt; has been saved to the database yet.&lt;br/&gt;&lt;br/&gt;
	&lt;/li&gt;
	&lt;li&gt;Create the file app/views/trains/_train_car.html.erb, add: &lt;br/&gt;
		&lt;code&gt;
&amp;lt;% content_tag("div", :id =&gt; "train_car_#{train_car.object_id}") do %&amp;gt;

	&amp;lt;% fields_for_train_car(train_car) do |f| %&amp;gt;

		&amp;lt;%= f.hidden_field(:car_id) %&amp;gt;

		&amp;lt;%= f.text_field(:position) %&amp;gt;

		&amp;lt;%= train_car.car.name %&amp;gt;

		&amp;lt;%= link_to_function("remove", "$('train_car_#{train_car.object_id}').remove()") %&amp;gt;

	&amp;lt;% end %&amp;gt;

&amp;lt;% end %&amp;gt;
		&lt;/code&gt;&lt;br/&gt;&lt;br/&gt;
		Once the `add` button has been pressed, this partial will be rendered into the DIV#train_cars container.  Also note that the `remove` button will remove this train car, but changes will only be saved once the `update` button has been pressed.&lt;br/&gt;&lt;br/&gt;
	&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;That's a lot of code!  Hopefully it all makes sense to you.  It's all been tested to work in the Rails 2.3.2 environment with Ruby 1.9.1.&lt;/p&gt;
&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-05-25T23:30:06Z</created-at>
    <description></description>
    <id type="integer">7</id>
    <keywords>ruby on rails, ruby, rails, multi model form, form, tutorial</keywords>
    <sub-title>lets build some trains to go with our rails!</sub-title>
    <title>Rails Multi-Model Forms Tutorial</title>
    <updated-at type="datetime">2009-07-04T21:39:51Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;Refactoring a project in Rails can be a handful, especially when the original database does not conform to Rails standards.  Lately, I've found it much quicker to simply redesign the data model, and migrate the data from the old database.&lt;/p&gt;
&lt;p&gt;I've had to tackle this problem a couple of times now, and feel like I've refined the process a great deal.  I don't know if this is the &lt;i&gt;correct&lt;/i&gt; way of doing things, I'm just saying, it works for me.&lt;/p&gt;
&lt;h4&gt;Step #1 - Map the legacy database&lt;/h4&gt;
&lt;br/&gt;
&lt;ol&gt;
	&lt;li&gt;Create a new file under config/initializers called legacy.rb&lt;/li&gt;
	&lt;li&gt;Create a new module, we'll call it "Legacy" &lt;br/&gt;
		&lt;code&gt;
			module Legacy&lt;br/&gt;
			end
		&lt;/code&gt;
	&lt;/li&gt;
	&lt;li&gt;Map your original database as best you can to rails models. &lt;br/&gt;
		&lt;code&gt;
			module Legacy&lt;br/&gt;&lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;class ActiveRecordLegacy &lt; ActiveRecord::Base &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;self.establish_connection( &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:adapter =&gt; 'mysql', &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:host =&gt; 'localhost', &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:username =&gt; 'username', &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:password =&gt; 'password', &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:database =&gt; 'legacy_database' &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;) &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;&lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;class LegacyTable &lt; ActiveRecordLegacy &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set_table_name :legacy_table &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set_primary_key :LegacyTableID &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;&lt;br/&gt;
			end
		&lt;/code&gt;
	&lt;/li&gt;
&lt;/ol&gt;
&lt;h4&gt;Step #2 - Create your migration script&lt;/h4&gt;
&lt;br/&gt;
&lt;ol&gt;
	&lt;li&gt;Create a new file model under app/models called migrate.rb &lt;br/&gt;
		&lt;code&gt;
			class Migrate&lt;br/&gt;
			end
		&lt;/code&gt;
	&lt;/li&gt;
	&lt;li&gt;Fill in your migration steps! &lt;br/&gt;
		&lt;code&gt;
			class Migrate&lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;def self.run &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Migrate.migrate_legacy_table() &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;&lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;def self.migrate_legacy_table &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Legacy::LegacyTable.find(:all).each { |record| &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Do some nifty migrations here &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;br/&gt;
			&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br/&gt;
			end
		&lt;/code&gt;
	&lt;/li&gt;
	&lt;li&gt;Load up the rails console: ruby ./script/console &lt;br/&gt;
		&lt;code&gt;
			&gt;&gt; Migrate.run&lt;br/&gt;
		&lt;/code&gt;
	&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Again, I don't know if this is the "correct" method, it's just how I do things.&lt;/p&gt;
&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-05-01T20:32:11Z</created-at>
    <description nil="true"></description>
    <id type="integer">4</id>
    <keywords nil="true"></keywords>
    <sub-title nil="true"></sub-title>
    <title>Migrating from a Legacy Datasource</title>
    <updated-at type="datetime">2009-05-01T20:32:17Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;I &lt;b&gt;love&lt;/b&gt; Ruby!  I've always loved Ruby, but today's discovery just reaffirms the validity of my devotion.&lt;/p&gt;
&lt;p&gt;I may need to watch my words in the future.  My girlfriend (like all girlfriends) doesn't like competition, and I wouldn't want any harm to come to my beloved Ruby.&lt;/p&gt;
&lt;p&gt;This handy little trick was picked up from Josh Susser over at &lt;a href='http://blog.hasmanythrough.com/'&gt;has_many :through&lt;/a&gt;.  His post &lt;a href='http://blog.hasmanythrough.com/2006/8/19/magic-join-model-creation'&gt;Magic join model creation&lt;/a&gt; goes over a lot of the details, but needed a little updating for Ruby 1.9.1 and Rails 2.0.&lt;/p&gt;
&lt;p&gt;We've all run across the situation: two tables, many to many association, but one model can have many 'types' of another.&lt;/p&gt;
&lt;p&gt;To better explain what I'm getting at take this example: &lt;/p&gt;
&lt;p&gt;&lt;code&gt;People &lt;-n --- n-&gt; Book&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;A person has many books, a book has many people.  Simple enough, right?  Well, sort of.  I'm sure those people associated with a book can have any number of affiliations that we may want to capture in our data model.&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;Author &lt;-n --- n-&gt; Book&lt;/code&gt;&lt;BR/&gt;
	&lt;code&gt;Editor &lt;-n --- n-&gt; Book&lt;/code&gt;&lt;BR/&gt;
	&lt;code&gt;Reviewer &lt;-n --- n-&gt; Book&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;All of these associations (author, editor, reviewer) are people, and all are associated with our book in some way or another, but creating the individual tables (book_authors, book_editors, book_reviewers) isn't my ideal solution to this problem, and there's a better way.&lt;/p&gt;
&lt;p&gt;Instead of creating all of those join tables, lets just make one: "book_people".&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
		create_table :book_people do |t| &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.column :person_id, :integer &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.column :book_id, :integer &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.column :person_type, :string &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.timestamps &lt;br/&gt;
		end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;In model "People".&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
		class Person &lt; ActiveRecord::Base &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :book_people, :dependent =&gt; :destroy &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :books, :through =&gt; :book_people &lt;br/&gt;
		end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;In model "Book"&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
		class Book &lt; ActiveRecord::Base &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :book_people, :dependent =&gt; :destroy &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :people, :through =&gt; :book_people, :uniq =&gt; true &lt;br/&gt;&lt;br/&gt;
	
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :authors, :through =&gt; :book_people, :source =&gt; :author, :conditions =&gt; ["`book_people`.contact_type = ?","Author"] do &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;def &lt;&lt;(author) &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BookPeople.send(:with_scope, :create =&gt; { :contact_type =&gt; "Author" } ) { self.concat author } &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;&lt;br/&gt;
		
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has_many :editors, :through =&gt; :book_people, :source =&gt; :editor, :conditions =&gt; ["`book_people`.contact_type = ?","Editor"] do &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;def &lt;&lt;(editor) &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BookPeople.send(:with_scope, :create =&gt; { :contact_type =&gt; "Editor" } ) { self.concat editor } &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end &lt;br/&gt;		
		end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;And finally, in model "BookPeople"&lt;/p&gt;
&lt;p&gt;
	&lt;code&gt;
		class BookPeople &lt; ActiveRecord::Base &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;belongs_to :person &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;belongs_to :book &lt;br/&gt;&lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;belongs_to :author, :class_name =&gt; "Person", :foreign_key =&gt; :person_id &lt;br/&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;belongs_to :editor, :class_name =&gt; "Person", :foreign_key =&gt; :person_id &lt;br/&gt;
		end
	&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;Amazing!  Three tables, infinite association types!  This model is superior to the many association tables model in a number of ways:&lt;/p&gt;
&lt;ol&gt;
	&lt;li&gt;If you simply want to find out all books a person is associated with, regardless of granularity, person.books works nicely!  One table, one query.&lt;/li&gt;
	&lt;li&gt;The Person model can be extended.  Associations such as person.authored_books, person.edited_books etc. are easy to add.&lt;/li&gt;
	&lt;li&gt;Clean database - I always try to get away with as few tables as possible.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now, I just hope this code works properly.&lt;/p&gt;
&lt;p&gt;-Butch&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-04-21T18:24:23Z</created-at>
    <description nil="true"></description>
    <id type="integer">2</id>
    <keywords nil="true"></keywords>
    <sub-title>fancy dancy many-to-many model associations!</sub-title>
    <title>Has_Many :Through - Setting a type on the join table</title>
    <updated-at type="datetime">2009-04-21T18:24:23Z</updated-at>
  </post>
  <post>
    <body>&lt;p&gt;
Well, my blog as officially gone live, and it's kind of fitting that my first post has to do with deploying Ruby on Rails websites.
&lt;/p&gt;
&lt;p&gt;
Ever received this error while deploying a Phusion Passenger site?
&lt;/p&gt;
&lt;center&gt;&lt;img src="/system/images/passenger_error_message.jpg" title="Phusion Passenger Error - no such file to load" alt="Phusion Passenger Error - no such file to load" /&gt;&lt;/center&gt;
&lt;p&gt;
I did!  The file existed, so I hope my bewilderment is forgivable.
&lt;/p&gt;
&lt;p&gt;
After 20 minutes of poking around, it finally occurred to me: check who owns the folder!  My Capitrano deploy makes whatever user account that deployed the release the owner of that release.  Passenger does not like this if that owner doesn't happen to be Apache. 
&lt;/p&gt;
&lt;p&gt;
The lesson for today?  Make sure that www-data, apache, or whatever user Apache is installed under owns all of the folders!
&lt;/p&gt;
&lt;p&gt;
-Butch
&lt;/p&gt;</body>
    <category-id type="integer">1</category-id>
    <created-at type="datetime">2009-04-16T00:36:35Z</created-at>
    <description></description>
    <id type="integer">1</id>
    <keywords>ruby on rails, ruby, rails, passenger</keywords>
    <sub-title>Don't forget to chown!</sub-title>
    <title>Passenger with a Capistrano Deployment</title>
    <updated-at type="datetime">2009-07-04T21:40:19Z</updated-at>
  </post>
</posts>
