<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rails Garden &#187; Rails Tools</title>
	<atom:link href="http://www.railsgarden.com/category/rails-tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsgarden.com</link>
	<description>A delicious blend of Ruby and Rails...</description>
	<lastBuildDate>Mon, 06 Apr 2009 05:05:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActiveRecord Drafts with has_draft</title>
		<link>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/</link>
		<comments>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 22:28:21 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails General]]></category>
		<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Rails Tools]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/</guid>
		<description><![CDATA[I ran into a problem a while back of creating draft copies of ActiveRecord models for the purpose of establishing a draft/live system.  I&#8217;ve since found a reason to resurrect this and publish it to GitHub and clean some things up.  Check out has_draft on GitHub.
has_draft allows for multiple &#8220;drafts&#8221; of a model [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem a while back of creating draft copies of ActiveRecord models for the purpose of establishing a draft/live system.  I&#8217;ve since found a reason to resurrect this and publish it to GitHub and clean some things up.  Check out <a href="https://github.com/railsgarden/has_draft/tree" title="has_draft">has_draft on GitHub</a>.</p>
<p>has_draft allows for multiple &#8220;drafts&#8221; of a model which can be useful when developing:</p>
<ul>
<li>Draft/Live Version of Pages, for examples</li>
<li>A workflow system whereby a live copy may need to be active while a draft copy is awaiting approval.</li>
</ul>
<p>The semantics of this as well as most of the inspiration comes from version_fu, an excellent plugin for a similar purpose of maintaining several &#8220;versions&#8221; of a model.</p>
<p>This was built to be able to be tacked on to existing models, so the data schema doesn&#8217;t need to change at all for the model this is applied to.  As such, drafts are actually stored in a nearly-identical table and there is a has_one relationship to this.  This separation allows the base model to really be treated just as before without having to apply conditions in queries to make sure you are really getting the &#8220;live&#8221; (non-draft) copy: Page.all will still only return the non-draft pages.  This separate table is backed by a model created on the fly as a constant on the original model class.  For example if a Page has_draft, a Page::Draft class will exist as the model for the page_drafts table.</p>
<p><strong>Basic Example:</strong>:</p>
<pre class="rails" name="code">
## First Migration (If Creating base model and drafts at the same time):
class InitialSchema &lt; ActiveRecord::Migration

  [:articles, :article_drafts].each do |table_name|
    create_table table_name, :force =&gt; true do |t|
      t.references :article if table_name == :article_drafts

      t.string :title
      t.text :summary
      t.text :body
      t.date :post_date
    end
  end
end

## Model Class
class Article &lt; ActiveRecord::Base
  has_draft
end

## Exposed Class Methods &amp; Scopes:
Article.draft_class
=&gt; Article::Draft
Article.with_draft.all
=&gt; (Articles that have an associated draft)
Article.without_draft.all
=&gt; (Articles with no associated draft)

## Usage Examples:
article = Article.create(
  :title =&gt; "My Title",
  :summary =&gt; "Information here.",
  :body =&gt; "Full body",
  :post_date =&gt; Date.today
)

article.has_draft?
=&gt; false

article.instantiate_draft!

article.has_draft?
=&gt; true

article.draft
=&gt; Article::Draft Instance

article.draft.update_attributes(
  :title =&gt; "New Title"
)

article.replace_with_draft!

article.title
=&gt; "New Title"

article.destroy_draft!

article.has_draft?
=&gt; false</pre>
<p><strong>Custom Options:</strong>:</p>
<pre class="rails" name="code">
## First Migration (If Creating base model and drafts at the same time):
class InitialSchema &lt; ActiveRecord::Migration

  [:articles, :article_copies].each do |table_name|
    create_table table_name, :force =&gt; true do |t|
      t.integer :news_article_id if table_name == :article_copies

      t.string :title
      t.text :summary
      t.text :body
      t.date :post_date
    end
  end

end

## Model Class
class Article &lt; ActiveRecord::Base
  has_draft :class_name =&gt; 'Copy', :foreign_key =&gt; :news_article_id, :table_name =&gt; 'article_copies'
end</pre>
<p><strong>Method Callbacks</strong>:<br />
There are three callbacks you can specify directly as methods.</p>
<pre class="rails" name="code">
class Article &lt; ActiveRecord::Base
  has_draft

  def before_instantiate_draft
    # Do Something
  end

  def before_replace_with_draft
    # Do Something
  end

  def before_destroy_draft
    # Do Something
  end
end</pre>
<p><strong>Block of Code Run for Draft Class</strong>:<br />
Because you don&#8217;t directly define the draft class, you can specify a block of code to be run in its<br />
context by passing a block to has_draft.</p>
<pre class="rails" name="code">
class Article &lt; ActiveRecord::Base
  belongs_to :user

  has_draft do
    belongs_to :last_updated_user

    def approve!
      self.approved_at = Time.now
      self.save
    end
  end

end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>named_scope with acts_as_tree</title>
		<link>http://www.railsgarden.com/2008/06/25/named_scope-with-acts_as_tree/</link>
		<comments>http://www.railsgarden.com/2008/06/25/named_scope-with-acts_as_tree/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 16:40:08 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails General]]></category>
		<category><![CDATA[Rails Techniques]]></category>
		<category><![CDATA[Rails Tools]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2008/06/25/named_scope-with-acts_as_tree/</guid>
		<description><![CDATA[I fairly often use the acts_as_tree plugin in my applications.  While acts_as_nested_set (and superior variants..) is more powerful, often times a simple two-level deep hierarchy is all I need and acts_as_tree is simple.  I&#8217;ve found the new named_scope functionality in Rails 2.1 to be very helpful when dealing with tree data structures.
Firstly, it&#8217;s somewhat rare [...]]]></description>
			<content:encoded><![CDATA[<p>I fairly often use the acts_as_tree plugin in my applications.  While acts_as_nested_set (and superior variants..) is more powerful, often times a simple two-level deep hierarchy is all I need and acts_as_tree is simple.  I&#8217;ve found the new named_scope functionality in Rails 2.1 to be very helpful when dealing with tree data structures.</p>
<p>Firstly, it&#8217;s somewhat rare that I have one single root node in the tree structure (which is apparently how it&#8217;s meant to be used).  Instead I&#8217;ll have multiple &#8220;parent&#8221; nodes designated with a NULL parent_id and children beneath.  In the past I&#8217;ve always done something like: find(:all, :conditions =&gt; {:parent_id =&gt; nil}) to grab the top entries.  Instead with nested set you can do this:</p>
<pre name="code" class="rails">
named_scope :top, :conditions =&gt; {:parent_id =&gt; nil}

#Then:
Category.top
</pre>
<p>Another common task when dealing with hierarchical categories is to query on the base object (products for example) for members that are &#8220;part of&#8221; that category.  Specifically, part of in a 2-level heirarchy simply means &#8220;where id = ? or parent_id = ?&#8221; on the joined categories table.  Because this involves a join it was somewhat clunky to do before.  Now with nested set, on the product model I can declare this:</p>
<pre name="code" class="rails">
named_scope :in_category, lambda {|c| {:include =&gt; [:category], :conditions =&gt; ["categories.id = ? OR categories.parent_id = ?", c, c]} }

# Then:
Product.in_category(3)
</pre>
<p>I would also highly encourage you all to check out <a href="http://railscasts.com/">RailsCasts</a> Episode 112 &#8220;Anonymous Scopes&#8221; by Ryan Bates where he outlines a pattern for handling conditions elegantly with searches using named_scopes in Rails 2.1, something I&#8217;ve always found to be lacking from Rails core and always resorted to using plugins like criteria_query.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2008/06/25/named_scope-with-acts_as_tree/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Configuring Passenger (mod_rails) on SliceHost with Ubuntu 7.10</title>
		<link>http://www.railsgarden.com/2008/04/12/configurating-passenger-mod_rails-on-slicehost-with-ubuntu-710/</link>
		<comments>http://www.railsgarden.com/2008/04/12/configurating-passenger-mod_rails-on-slicehost-with-ubuntu-710/#comments</comments>
		<pubDate>Sat, 12 Apr 2008 19:30:34 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails General]]></category>
		<category><![CDATA[Rails Tools]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2008/04/12/configurating-passenger-mod_rails-on-slicehost-with-ubuntu-710/</guid>
		<description><![CDATA[I had an opportunity to play around with Phusion Passenger (mod_rails) which was just released yesterday.  Before yesterday all that was available was an introductory video showing Passenger being configured, and I was quite impressed by it&#8217;s ease.  Clearly one of the problems with Rails as a major platform right now is it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I had an opportunity to play around with <a href="http://www.modrails.com/">Phusion Passenger (mod_rails)</a> which was just released yesterday.  Before yesterday all that was available was an introductory video showing Passenger being configured, and I was quite impressed by it&#8217;s ease.  Clearly one of the problems with Rails as a major platform right now is it&#8217;s hosting situation.  Currently the best solution is to proxy HTTP requests from Apache or Nginx to a cluster of mongrels, which is tricky to set up and somewhat tedious &#8211; certainly not as as easy as a mod_php in Apache where things &#8220;just work&#8221;.  Phusion set out to overcome this problem and release a module for Apache that hosts Rails applications easily.  Here is what took me from a clean Ubuntu 7.10 slice from SliceHost to a working server with Passenger:<strong>Starting Out</strong>To start out, I used the following *excellent* SliceHost articles to get some of the basic packages installed such as Ruby and MySQL:<a href="http://articles.slicehost.com/2007/11/6/ubuntu-gutsy-setup-page-1">Ubuntu Setup &#8211; Part 1</a><a href="http://articles.slicehost.com/2007/11/6/ubuntu-gutsy-setup-page-2">Ubuntu Setup &#8211; Part 2</a><a href="http://articles.slicehost.com/2007/11/23/ubuntu-gutsy-mysql-and-ror">MySQL and Rails</a><a href="http://articles.slicehost.com/2007/11/23/ubuntu-gutsy-installing-apache-and-php5">Apache and PHP</a><a href="http://articles.slicehost.com/2007/11/23/ubuntu-gutsy-apache-virtual-hosts">Apache Virtual Hosts</a><a href="http://articles.slicehost.com/2007/9/18/apache-virtual-hosts-permissions">Virtual Hosts &amp; Permissions</a>Note my permissions gotcha below &#8211; you may not want to strictly follow their permissions advice (where the global does not have read permission).I also installed sqlite3 as this is now the default database package used in Rails 2.0:
<pre class="ruby:nogutter:nocontrols" name="code">$ sudo aptitude install sqlite3$ sudo gem install sqlite3-ruby</pre>
<p><strong>Installing the Passenger Apache Module</strong>Using the simple <a href="http://www.modrails.com/install.html">instructions</a> on the Passenger website, I ran:
<pre class="ruby:nogutter:nocontrols" name="code">$ sudo gem install passenger$ passenger-install-apache2-module</pre>
<p>The passenger-install-apache2-module binary walks you through the installation of the Passenger.  On the first run, it told me I didn&#8217;t have some software that was required:
<pre class="ruby:nogutter:nocontrols" name="code">* GNU C++ compiler... found* Ruby development headers... found* OpenSSL support for Ruby... found* RubyGems... found* Rake... found at /usr/bin/rake* Apache 2... found at /usr/sbin/apache2* Apache 2 development headers... not found* Apache Portable Runtime (APR) development headers... not found* fastthread... foundSome required software is not installed.But don't worry, this installer will tell you how to install them....* To install Apache 2 development headers:Please run apt-get install apache2-prefork-dev as root.* To install Apache Portable Runtime (APR) development headers:Please run apt-get install libapr1-dev as root.</pre>
<p>Clearly the installer detected I was running Debian-based Linux and told me exactly what to do.  No compile errors or nasty dependency issues, just simple and clear instructions.  After installing the requisite software the installer compiled the Apache module and spit out this:
<pre class="ruby:nogutter:nocontrols" name="code">The Apache 2 module was successfully installed.Please edit your Apache configuration file, and add these lines:LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/ext/apache2/mod_passenger.soRailsSpawnServer /usr/lib/ruby/gems/1.8/gems/passenger-1.0.1/bin/passenger-spawn-serverRailsRuby /usr/bin/ruby1.8After you restart Apache, you are ready to deploy any numberof Ruby on Rails applications on Apache, without any furtherRuby on Rails-specific configuration!</pre>
<p>Note that it didn&#8217;t find or attempt to find where apache modules are installed as the actual compiled modules is kept within the Ruby gems directory, it just told me what lines to include in my Apache configuration file.  The last part of the installation says:
<pre class="ruby:nogutter:nocontrols" name="code">Deploying a Ruby on Rails application: an example.Suppose you have a Ruby on Rails application in /somewhere.Add a virtual host to your Apache configuration file,and set its DocumentRoot to /somewhere/public, like this:&lt;VirtualHost&gt;      ServerName www.yourhost.com      DocumentRoot /somewhere/public&lt;/VirtualHost&gt;And that's it!</pre>
<p>Now this is pretty cool&#8230; Passenger doesn&#8217;t even require virtual hosts to configure themselves as Rails applications.  It will automatically detect a Rails application (I&#8217;m unsure of the actual criteria here) based on the DocumentRoot that you provide.So I added the necessary lines specified above to /etc/apache2/apache2.conf to load the module.  Note that some deployments of Apache have their own way of managing modules; in Ubuntu there is a mods-available directory with individual configuration files for each module.  You could split up the above commands to follow the convention for your system of choice, but for my purposes I just dumped the three lines into apache2.conf.Then I created a new Rails application and pointed DocumentRoot to the Rails public directory.  Restart apache, and the site should then immediately work with no further configuration.<strong>Permissions Gotcha &#8211; Important!</strong>If you get:
<pre class="ruby:nogutter:nocontrols" name="code">RAILS_ROOT is not a directory.</pre>
<p>The first time I got to this point I was running into an extremely obscure problem &#8211; Rails was reporting that RAILS_ROOT wasn&#8217;t a directory.  After some debugging which involved hacking up the gem source code of both Rails and Passenger, I realized it was a permissions issue (what do you know). Passenger reports that it will run your Rails application as whatever user owns environment.rb.  However, it will set the effective group to that user&#8217;s primary UNIX group, NOT the group that environment.rb is owned by.  In my case, I set the group of all of the web files to www-data and no read or execute permissions whatsoever for other.  I figured this would be sufficient since Apache runs as www-data.  But in my case when Passenger got spawned, it got spawned running as bhughes:bhughes NOT bhughes:www-data, so it effectively couldn&#8217;t find the RAILS_ROOT directory and didn&#8217;t think it existed.  You can design your permissions scheme however you want, but make sure you keep this in mind.  I have an <a href="http://code.google.com/p/phusion-passenger/issues/detail?id=13">enhancement in Passenger&#8217;s ticketing system</a> for fixing this and allowing you to manually set the effective group (you already can explicitly set the effective user through the RailsUser option).<strong>More Passenger Configuration</strong>The Passenger <a href="http://www.modrails.com/documentation/Users%20guide.html">User&#8217;s Guide</a> is pretty good and includes some more specific configuration options you can choose, but the default appears to work fine.  The Rails environment is by default set to &#8220;production&#8221; though this can be changed (sadly, only server-wide, not on a virtual-host basis &#8211; but there is an enhancement in the issue tracker for this feature).<strong>Restarting Your App</strong>Restarting a Rails app hosted by Passenger is easy &#8211; simply touch a file called tmp/restart.txt within the Rails application root:
<pre class="ruby:nogutter:nocontrols" name="code">$ touch tmp/restart.txt</pre>
<p><strong>Capistrano</strong>Brian Ketelsen pointed out this Capistrano configuration which uses the touch tmp/restart.txt approach to restart and removes the .htaccess file which can cause issues:
<pre class="ruby" name="code">namespace :deploy do  task :start, :roles =&gt; :app do  end  task :stop, :roles =&gt; :app do  end  task :restart, :roles =&gt; :app do    run "touch #{release_path}/tmp/restart.txt"  end  task :after_update_code, :roles =&gt; :app do    run "rm -rf #{release_path}/public/.htaccess"  endend</pre>
<p><strong>Diagnosing Problems</strong>One of the absolutely wonderful things about Passenger is that problem diagnostics (regarding starting your Rails app) is fairly easy because Passenger includes its own nice error pages:<a href="http://izumi.plan99.net/blog/wp-content/uploads/2008/03/database_error.html">Database Error Example</a><a href="http://izumi.plan99.net/blog/wp-content/uploads/2008/03/framework_init_error.html">Framework Initialization Example</a><strong>Memory Management</strong>One of the issues with mongrel or thin is that they are constantly running and sucking up memory, even for sites that get very little traffic.  This leads to a situation where hosting very low-traffic sites is very inefficient from a system resources perspective.  This isn&#8217;t the case with PHP where memory usage spikes during traffic but is otherwise unused, allowing multiple sites to leverage the on and offs and maintain a pretty efficient hosting platform as far as memory is concerned.  One of the great feature of Passenger is that you can configure it to automatically shut down your Rails application if it is idle for a certain period of time.  Doing this obviously exerts a cost then on the next hit, but this may be an acceptable tradeoff for a server hosting many low-traffic sites.  The configuration option is RailsPoolIdleTime and there is more information on this in the user guide.<strong>Conclusion</strong>Obviously my introduction here doesn&#8217;t go in-depth into performance and ability of Passenger to scale.  Time will tell as people begin using this whether it is a viable solution for Rails hosting, but for me installing this and getting it to work was dirt simple, at least compared to the mod_proxy_balancer setup with mongrel clusters I have used in the past.  I&#8217;m not quite ready to actually host my sites using this but I&#8217;ll definitely be keeping my eye on this &#8211; it could be a major step forward in the proliferation of Rails hosting.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2008/04/12/configurating-passenger-mod_rails-on-slicehost-with-ubuntu-710/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>New MacBook!</title>
		<link>http://www.railsgarden.com/2007/12/28/new-macbook/</link>
		<comments>http://www.railsgarden.com/2007/12/28/new-macbook/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 15:07:41 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Rails Tools]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2007/12/28/new-macbook/</guid>
		<description><![CDATA[This Christmas my parents got me an Apple giftcard which I promptly used to buy a new 2.2Ghz MacBook. With next day shipping, I had it in my hands at 10:30 yesterday morning (27th) – pretty fast!
All day yesterday I was setting stuff up (and to some extent today) and I think I’ve finally got [...]]]></description>
			<content:encoded><![CDATA[<p>This Christmas my parents got me an Apple giftcard which I promptly used to buy a new 2.2Ghz MacBook. With next day shipping, I had it in my hands at 10:30 yesterday morning (27th) – pretty fast!</p>
<p>All day yesterday I was setting stuff up (and to some extent today) and I think I’ve finally got everything moved over. I also invested in 4 gigs of memory from Newegg for only $110. Because of the small screen, I’ve found Leopard Spaces invaluable. I still have quite a bit to learn, but I’m glad I made the switch. I can finally start developing with TextMate.</p>
<p><img src="http://www.railsgarden.com/wp-content/uploads/2008/01/mymac.png" alt="My New MacBook" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2007/12/28/new-macbook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
