<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>All about Rails - Home</title>
  <id>tag:blog.viarails.net,2009:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.7.3">Mephisto Noh-Varr</generator>
  <link href="http://blog.viarails.net/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://blog.viarails.net/" rel="alternate" type="text/html"/>
  <updated>2009-01-29T20:08:07Z</updated>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2009-01-29:78</id>
    <published>2009-01-29T19:46:00Z</published>
    <updated>2009-01-29T20:08:07Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2009/1/29/disabling-callbacks-in-an-activerecord-data-migration" rel="alternate" type="text/html"/>
    <title>Disabling callbacks in an ActiveRecord data migration</title>
<content type="html">
            &lt;p&gt;It&#8217;s often useful to disable ActiveRecord callbacks such as :after_save when migrating data. It&#8217;s rather easy to do:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
Foo.after_create.clear
Foo.after_save.clear
... migration code ...
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you only need to disable certain actions, it&#8217;s also trivial:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;
Foo.after_save.reject! {|callback| callback.method.to_s == 'some_method_name' }
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This sort of thing should never be used in application code, if you&#8217;re doing this then your model is broken. However, it&#8217;s great for data migrations.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2009-01-09:76</id>
    <published>2009-01-09T16:49:00Z</published>
    <updated>2009-01-09T16:54:02Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2009/1/9/irc-question-of-the-day" rel="alternate" type="text/html"/>
    <title>IRC question of the day</title>
<content type="html">
            &lt;h3&gt;Question:&lt;/h3&gt;
&lt;pre&gt;
_adc: I'm doing compiling some reports on various database records based in whatever
 model the user chooses... Is there a better way to figure out which models are in 
existence than http://pastie.org/356630 ?
_adc: I'm looking for an array of all models currently in existence.
&lt;/pre&gt;
&lt;h3&gt;
Answer:
&lt;/h3&gt;

ActiveRecord::Base.subclasses_of(ActiveRecord::Base)

&lt;pre&gt;
&lt;code&gt;
~/projects/learnhub$ ./script/console 
Loading development environment (Rails 2.1.1)
&gt;&gt; ActiveRecord::Base.subclasses_of(ActiveRecord::Base).collect {|c| c.to_s }
=&gt; [&quot;CGI::Session::ActiveRecordStore::Session&quot;, &quot;ContactPostCreationNotification&quot;, .....]
&lt;/code&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-12-31:75</id>
    <published>2008-12-31T15:00:00Z</published>
    <updated>2009-06-05T15:39:04Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/12/31/three-simple-scripts-for-passenger-mod_rails-management" rel="alternate" type="text/html"/>
    <title>Three simple scripts for Passenger (mod_rails) management</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://www.modrails.com/'&gt;Phusion Passenger&lt;/a&gt; is the best option for hosting Ruby based web apps (anything that uses &lt;a href='http://rack.rubyforge.org/'&gt;Rack&lt;/a&gt; is compatible). When used with &lt;a href='http://www.rubyenterpriseedition.com/'&gt;Ruby EE&lt;/a&gt; to serve Ruby on Rails applications it will consume up to 33% less memory than other servers such as Mongrel.&lt;/p&gt;


	&lt;p&gt;Not everything is kittens and rainbows however, as Passenger won&#8217;t prevent your app from choking your server if you consume too much memory (via memory leaks in your app, etc). Schedule &lt;a href='http://gist.github.com/41713'&gt;this script&lt;/a&gt; to run every couple of minutes to kill any Passenger processes are consuming too much memory. Passenger will automatically start new ones as needed. You&#8217;ll have to figure out for yourself how much is &#8220;too much&#8221;. In my case it&#8217;s 200 MB.&lt;/p&gt;


	&lt;p&gt;It&#8217;s also useful to know how many process Passenger is using, so you can increase the MaxPoolSize when traffic increases. I use &lt;a href='http://munin.projects.linpro.no/'&gt;Munin&lt;/a&gt; to track system statistics and have written two wrappers around the Passenger system tools. The first &lt;a href='http://gist.github.com/37525'&gt;wraps passenger-memory-stats&lt;/a&gt; and the second &lt;a href='http://gist.github.com/37542'&gt;wraps passenger-status&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;The result are graphs such as these:&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://blog.viarails.net/assets/2008/12/29/passenger_memory-week.png' alt='' /&gt;
&lt;img src='http://blog.viarails.net/assets/2009/6/5/passenger-monthly.png' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;Munin graphs can be extremely helpful when tracking down system performance issues. I highly recommend it.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-12-29:74</id>
    <published>2008-12-29T19:13:00Z</published>
    <updated>2009-01-06T16:01:29Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/12/29/texhub-a-math-equation-service" rel="alternate" type="text/html"/>
    <title>TexHub: A math equation service</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://heycarsten.com'&gt;Carsten&lt;/a&gt; and I recently launched &lt;a href='http://texhub.com'&gt;TexHub&lt;/a&gt; which is a little service for serving up latex equations as images. It&#8217;s pretty simple, just Base64 + &lt;span class='caps'&gt;URI&lt;/span&gt; encode latex string and append it to a &lt;span class='caps'&gt;URL&lt;/span&gt;. It&#8217;s extracted from our math equations feature on &lt;a href='http://learnhub.com'&gt;Learnhub&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;For example:&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://texhub.com/b/ZT1NQ14y'&gt;e=MC&lt;sup&gt;2&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://texhub.com/b/ZT1NQ14y' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;A more complex example:
&lt;a href='http://texhub.com/b/Rih4KSA9IFxpbnRfey1caW5mdHl9XnthfSBmKHQpZHQgKyBcaW50X3thfV57%0AeH0gZih0KWR0t'&gt;F(x) = \\int_{-\\infty}&lt;/sup&gt;{a} f(t)dt + \\int_{a}^{x} f(t)dt&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;&lt;img src='http://texhub.com/b/Rih4KSA9IFxpbnRfey1caW5mdHl9XnthfSBmKHQpZHQgKyBcaW50X3thfV57%0AeH0gZih0KWR0' /&gt;&lt;/p&gt;


	&lt;p&gt;We haven&#8217;t done much with the front end yet as we&#8217;ll primarily be using it as a service, so the focus has been on speed. I wrote the web interface using &lt;a href='http://sinatra.rubyforge.org/'&gt;Sinatra&lt;/a&gt; which turned out to be perfect for this sort of app. I plan on writing more micro apps in the future with Sinatra, perhaps the next thing will be the Ruby version of &lt;a href='http://www.scs.ryerson.ca/~mth110/Tilomino/'&gt;Tilomino&lt;/a&gt; which has been sitting untouched for two years&#8230;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-08-25:70</id>
    <published>2008-08-25T21:45:00Z</published>
    <updated>2008-12-31T15:36:35Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/8/25/irony" rel="alternate" type="text/html"/>
    <title>Irony</title>
<content type="html">
            &lt;p&gt;&lt;img src='http://blog.viarails.net/assets/2008/8/25/Picture_12.png' alt='' /&gt;&lt;/p&gt;


	&lt;p&gt;Glad to know that MS still considers that &lt;span class='caps'&gt;IE 6&lt;/span&gt;.0 is a &#8216;recent browser&#8217; (it&#8217;s 7 years old, which is ancient in internet years).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-08-11:54</id>
    <published>2008-08-11T19:19:00Z</published>
    <updated>2008-12-31T15:36:42Z</updated>
    <category term="Archive"/>
    <category term="rails benchmark"/>
    <link href="http://blog.viarails.net/2008/8/11/ruby-vm-shootout" rel="alternate" type="text/html"/>
    <title>Ruby VM shootout on Rock, Paper, Scissors!</title>
<content type="html">
            &lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; I&#8217;ve added benchmark numbers for JRuby 1.1.3&lt;/p&gt;


	&lt;p&gt;I&#8217;ve been messing with &lt;a href='http://github.com/wmoxam/rubyrps/tree/master'&gt;RubyRPS&lt;/a&gt;  (Rock, paper scissors) lately, it&#8217;s a ton of fun!
I thought it might be cool to benchmark the progression of Rubinius, and that little experiment turned into a full blown ruby interpreter shootout. Sure, this is hardly fair since &lt;span class='caps'&gt;RPS&lt;/span&gt; doesn&#8217;t use much in the way of complicated stuff (lots of loops over array, etc), but most benchmarks are bullshit anyways. I just want my &lt;span class='caps'&gt;RPS&lt;/span&gt; bots to run fast!&lt;/p&gt;


	&lt;p&gt;Here are the versions I tested.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href='http://ruby-lang.org'&gt;ruby&lt;/a&gt; 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] (macports)&lt;/li&gt;
		&lt;li&gt;ruby 1.8.7 (2008-06-20 patchlevel 22) [i686-darwin9.3.0] (macports)&lt;/li&gt;
		&lt;li&gt;ruby 1.9.0 (2008-03-01 revision 15664) [i686-darwin9.4.0] (macports)&lt;/li&gt;
		&lt;li&gt;ruby 1.9.0 (2008-07-25 revision 18218) [i686-darwin9.4.0] (snapshot 1.9.0.3)&lt;/li&gt;
		&lt;li&gt;&lt;a href='http://rubini.us/'&gt;Rubinius&lt;/a&gt; 0.9.0 (ruby 1.8.6 compatible) (ffb998bf5) (08/07/2008) [i686-apple-darwin9.4.0] (from trunk)&lt;/li&gt;
		&lt;li&gt;&lt;a href='http://jruby.codehaus.org/'&gt;JRuby&lt;/a&gt; 1.8.6 (2008-08-07 rev 6555) [i386-jruby1.1.1] (macports)&lt;/li&gt;
		&lt;li&gt;&lt;a href='http://www.macruby.org/'&gt;MacRuby&lt;/a&gt; version 0.3 (ruby 1.9.0 2008-06-03) [universal-darwin9.4] (downloaded binary)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Benchmarking is rather simple:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
# time ruby ./run.rb
# time ruby1.8.7 ./run.rb
# time ruby 1.9 ./run.rb
# time rubinius ./run.rb
# time jruby ./run.rb
# time macruby ./run.rb
&lt;/code&gt;
&lt;/pre&gt;

	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Interpreter&lt;/td&gt;
			&lt;td&gt;Memory Usage&lt;/td&gt;
			&lt;td&gt;Real&lt;/td&gt;
			&lt;td&gt;User&lt;/td&gt;
			&lt;td&gt;System&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.8.6&lt;/td&gt;
			&lt;td&gt;3MB&lt;/td&gt;
			&lt;td&gt;0m36.183s&lt;/td&gt;
			&lt;td&gt;0m35.426s&lt;/td&gt;
			&lt;td&gt;0m0.241s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.8.7&lt;/td&gt;
			&lt;td&gt;10MB&lt;/td&gt;
			&lt;td&gt;0m17.778s&lt;/td&gt;
			&lt;td&gt;0m17.086s&lt;/td&gt;
			&lt;td&gt;0m0.154s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.9.0&lt;/td&gt;
			&lt;td&gt;11MB&lt;/td&gt;
			&lt;td&gt;1m17.092s&lt;/td&gt;
			&lt;td&gt;0m53.843s&lt;/td&gt;
			&lt;td&gt;0m22.234s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.9.0.3&lt;/td&gt;
			&lt;td&gt;10MB&lt;/td&gt;
			&lt;td&gt;0m10.350s&lt;/td&gt;
			&lt;td&gt;0m9.911s&lt;/td&gt;
			&lt;td&gt;0m0.111s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;JRuby 1.1.1&lt;/td&gt;
			&lt;td&gt;28MB&lt;/td&gt;
			&lt;td&gt;0m18.023s&lt;/td&gt;
			&lt;td&gt;0m16.198s&lt;/td&gt;
			&lt;td&gt;0m0.417s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;JRuby 1.1.3&lt;/td&gt;
			&lt;td&gt;28MB&lt;/td&gt;
			&lt;td&gt;0m18.622s&lt;/td&gt;
			&lt;td&gt;0m17.482s&lt;/td&gt;
			&lt;td&gt;0m0.321s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Rubinius&lt;/td&gt;
			&lt;td&gt;26MB&lt;/td&gt;
			&lt;td&gt;0m42.836s&lt;/td&gt;
			&lt;td&gt;0m41.556s&lt;/td&gt;
			&lt;td&gt;0m0.479s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;MacRuby 0.3&lt;/td&gt;
			&lt;td&gt;476MB&lt;/td&gt;
			&lt;td&gt;0m42.471s&lt;/td&gt;
			&lt;td&gt;0m40.302s&lt;/td&gt;
			&lt;td&gt;0m0.987s&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;p&gt;How about a threaded run just for fun?&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
# time ruby ./run.rb --threaded
# time ruby1.8.7 ./run.rb --threaded
# time ruby 1.9 ./run.rb --threaded
# time rubinius ./run.rb --threaded
# time jruby ./run.rb --threaded
# time macruby ./run.rb --threaded
&lt;/code&gt;
&lt;/pre&gt;

	&lt;table&gt;
		&lt;tr&gt;
			&lt;td&gt;Interpreter&lt;/td&gt;
			&lt;td&gt;Memory Usage&lt;/td&gt;
			&lt;td&gt;Real&lt;/td&gt;
			&lt;td&gt;User&lt;/td&gt;
			&lt;td&gt;System&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.8.6&lt;/td&gt;
			&lt;td&gt;4MB&lt;/td&gt;
			&lt;td&gt;1m12.343s&lt;/td&gt;
			&lt;td&gt;1m9.703s&lt;/td&gt;
			&lt;td&gt;0m0.695s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.8.7&lt;/td&gt;
			&lt;td&gt;13MB&lt;/td&gt;
			&lt;td&gt;0m18.019s&lt;/td&gt;
			&lt;td&gt;0m17.561s&lt;/td&gt;
			&lt;td&gt;0m0.188s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.9&lt;/td&gt;
			&lt;td&gt;13MB&lt;/td&gt;
			&lt;td&gt;1m18.341s&lt;/td&gt;
			&lt;td&gt;0m54.516s&lt;/td&gt;
			&lt;td&gt;0m21.947s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Ruby 1.9.0.3&lt;/td&gt;
			&lt;td&gt;13MB&lt;/td&gt;
			&lt;td&gt;0m10.505s&lt;/td&gt;
			&lt;td&gt;0m10.013s&lt;/td&gt;
			&lt;td&gt;0m0.126s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;JRuby 1.1.1&lt;/td&gt;
			&lt;td&gt;34MB&lt;/td&gt;
			&lt;td&gt;0m17.048s&lt;/td&gt;
			&lt;td&gt;0m22.013s&lt;/td&gt;
			&lt;td&gt;0m1.890s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;JRuby 1.1.3&lt;/td&gt;
			&lt;td&gt;31MB&lt;/td&gt;
			&lt;td&gt;0m15.630s&lt;/td&gt;
			&lt;td&gt;0m22.580s&lt;/td&gt;
			&lt;td&gt;0m1.475s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;Rubinus&lt;/td&gt;
			&lt;td&gt;52MB&lt;/td&gt;
			&lt;td&gt;0m43.745s&lt;/td&gt;
			&lt;td&gt;0m41.893s&lt;/td&gt;
			&lt;td&gt;0m0.591s&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;td&gt;MacRuby 0.3&lt;/td&gt;
			&lt;td&gt;Error!&lt;/td&gt;
			&lt;td&gt;stack level too deep (SystemStackError)&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/table&gt;




	&lt;h2&gt;Conclusion&lt;/h2&gt;


	&lt;p&gt;Ruby 1.9 runs &lt;span class='caps'&gt;RPS&lt;/span&gt; &lt;strong&gt;really fast&lt;/strong&gt;. You may have to compile it yourself for now if you&#8217;re on a Mac, since the version in Macports does have some issues. Also, I&#8217;d recommend that anyone who is still running &lt;a href='http://rubyonrails.org'&gt;Rails&lt;/a&gt; apps on 1.8.6 to move to 1.8.7 to for an increase in speed. We&#8217;ve been using 1.8.7 to run &lt;a href='http://learnhub.com'&gt;Learnhub&lt;/a&gt; for a few weeks now and have noticed a decrease in memory leaks as well. Be sure to check for compatibility with your Rails app though, 1.8.7 doesn&#8217;t work with some older versions of Rails.&lt;/p&gt;


	&lt;h2&gt;Surprises&lt;/h2&gt;


	&lt;p&gt;I was surprised that Jruby did so well. It performs just as well as &lt;span class='caps'&gt;MRI 1&lt;/span&gt;.8.7 (with about double the memory usage). I was expecting the interpreter startup time to hold Jruby back, but that didn&#8217;t turn out to be the case. Rubinius also performed a bit better than expected (they&#8217;ve been working on correctness rather than speed thus far). It wasn&#8217;t much slower than &lt;span class='caps'&gt;MRI 1&lt;/span&gt;.8.6, although it consumed far more &lt;span class='caps'&gt;RAM&lt;/span&gt;. My understanding is that the VM is currently being rewritten, so I&#8217;ll run a few more tests when it&#8217;s released.&lt;/p&gt;


	&lt;p&gt;MacRuby is also an interesting project, with a goal of porting &lt;span class='caps'&gt;MRI 1&lt;/span&gt;.9 to run directly on top of Mac &lt;span class='caps'&gt;OS X&lt;/span&gt; core technologies such as the Objective-C common runtime and garbage collector, and the CoreFoundation framework. It&#8217;s very much a work in progress at this point.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-08-06:52</id>
    <published>2008-08-06T05:45:00Z</published>
    <updated>2008-12-31T15:36:48Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/8/6/microsoft-doesn-t-care-about-the-web" rel="alternate" type="text/html"/>
    <title>Microsoft Doesn't Care About the Web</title>
<content type="html">
            I just wanted to read some comments on a MSN Money article. I got this:

&lt;div&gt;
&lt;img src='http://blog.viarails.net/assets/2008/8/6/Picture_6.png' /&gt;
&lt;/div&gt;
&lt;p&gt;
Microsoft FAIL.
&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-07-28:48</id>
    <published>2008-07-28T01:40:00Z</published>
    <updated>2008-12-31T15:36:55Z</updated>
    <category term="Archive"/>
    <category term="rails acts_as_paranoid counter_cache gotcha"/>
    <link href="http://blog.viarails.net/2008/7/28/acts_as_paranoid-and-counter_cache" rel="alternate" type="text/html"/>
    <title>acts_as_paranoid and counter_cache</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000980'&gt;Counter caches&lt;/a&gt; in RoR eliminate the need to issue an sql query to determine the size of an associated collection. Using counter cache is quite simple. Here&#8217;s an example:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Author &amp;lt; ActiveRecord::Base
  has_many :books
end

class Book &amp;lt; ActiveRecord::Base
  belongs_to :author, :counter_cache =&amp;gt; true
end

add_column :authors, :books_count, :integer

Author.first.books.count  # gets the value from the books_count column, 
                              # rather than doing an sql count
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;There&#8217;s a problem if you use this alongside &lt;a href='http://ar-paranoid.rubyforge.org/'&gt;acts_as_paranoid&lt;/a&gt;. &lt;span class='caps'&gt;AAP&lt;/span&gt; is a plugin that provides &#8216;soft delete&#8217; functionality. In order to achieve this, &lt;span class='caps'&gt;AAP&lt;/span&gt; overrides AR finder methods to check the &#8216;deleted_at&#8217; column to determine if a record has been &#8216;deleted&#8217;. Due to a condition placed on the count (counting only undeleted objects), the counter cache is not used and instead an sql statement is issued.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Author &amp;lt; ActiveRecord::Base
  has_many :books
end

class Book &amp;lt; ActiveRecord::Base
  acts_as_paranoid
  belongs_to :author, :counter_cache =&amp;gt; true
end

Author.first.books.count # SELECT count(*) AS count_all FROM `books` 
                             # WHERE (`books`.author_id = 11524) AND 
                             # (`books`.deleted_at IS NULL OR 
                             # `books`.deleted_at &amp;gt; '2008-07-27 13:27:22') 
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Deleting an object from a &#8216;paranoid&#8217; collection will still do the right thing (decrement the counter cache), making this easy to fix. You just have to add your own count method for the collection.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Author &amp;lt; ActiveRecord::Base
  has_many :books do
    def count
       books_count
    end
  end
end

Author.first.books.count  # calls the count method, returning
                       # the books counter 
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;This is also published on &lt;a href='http://rails.learnhub.com/lesson/page/3397-actsasparanoid-and-countercache'&gt;Learnhub&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-07-22:43</id>
    <published>2008-07-22T23:16:00Z</published>
    <updated>2008-12-31T15:37:00Z</updated>
    <category term="Archive"/>
    <category term="rails nginx haproxy"/>
    <link href="http://blog.viarails.net/2008/7/22/nginx-haproxy-gotcha" rel="alternate" type="text/html"/>
    <title>Nginx + HaProxy gotcha</title>
<content type="html">
            &lt;p&gt;I&#8217;ve recently switched from using a straight up Apache mod_proxy + mongrel setup to Nginx + HaProxy +mongrel. Basically, mod_proxy is dumb as shit, and should never be used with mongrel + rails, especially with better options like &lt;a href='http://www.modrails.com/'&gt;Passenger&lt;/a&gt; and &lt;a href='http://haproxy.1wt.eu/'&gt;HaProxy&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Why does mod_proxy suck? Take a look for yourself:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
[8030/5/247]: handling 127.0.0.1: GET
[8031/6/134]: handling 127.0.0.1: GET
[8032/2/160]: handling 127.0.0.1: GET
[8033/0/93]: idle
[8034/10/128]: handling 127.0.0.1: GET
[8036/2/124]: handling 127.0.0.1: GET
[8035/0/110]: idle
[8037/0/101]: idle
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Look at this garbage. There are three idle mongrels even though the other five have 25 requests to be processed.&lt;/p&gt;


	&lt;p&gt;Whats great about HaProxy is that it will queue up requests and only distribute them to idle mongrels. It translates into way better response times under load. Add Nginx, which is way easier to configure that Apache, allowing me to easily setup things like sending known &#8216;slow&#8217; requests to separate mongrels and it&#8217;s a big win. As a bonus, haproxy has a great status tool, which gives a quick overview of your proxy clusters.&lt;/p&gt;


&amp;lt;image src='http://blog.viarails.net/assets/2008/7/22/haproxy.png' /&gt;

	&lt;p&gt;&lt;br /&gt;
&lt;h2&gt; One little problem &#8230; &lt;/h2&gt;
The gotcha when I initially switched over was that &lt;a href='http://agilewebdevelopment.com/plugins/exception_notifier'&gt;exception_notifier&lt;/a&gt; stopped sending me error emails, in fact the errors were being displayed to the user. What the hell?&lt;/p&gt;


	&lt;p&gt;Exception notifier looks at the incoming IP address. If it&#8217;s local (aka: 127.0.0.1), it&#8217;s assumed that the user is a developer, so it won&#8217;t email the exception to you. haproxy was sending nginx&#8217;s IP address, rather than the forwarded client address. The problem was that I had configured both nginx and haproxy to forward headers.&lt;/p&gt;


haproxy.conf:
&lt;pre&gt;
&lt;code&gt;
option          forwardfor    # enable insert of X-Forwarded-For headers
&lt;/code&gt;
&lt;/pre&gt;

nginx.conf 
&lt;pre&gt;
&lt;code&gt;
proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;Removing the header forwarding in the haproxy configuration solved the problem. You only need to set the X-Forwarded header in Nginx (or Apache or Pound or whatever front end you choose). Setting it in Haproxy as well will over write the header with one containing the IP of the front end. Since I have Nginx and Haproxy running on the same machine, that IP is 127.0.0.1, which broke the exception_notifier.&lt;/p&gt;


	&lt;p&gt;This is also published on &lt;a href='http://rails.learnhub.com/lesson/page/3398-nginx--haproxy-usage-and-a-gotcha'&gt;Learnhub&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-07-21:42</id>
    <published>2008-07-21T05:18:00Z</published>
    <updated>2008-12-31T15:32:47Z</updated>
    <category term="Archive"/>
    <category term="ruby fringe"/>
    <link href="http://blog.viarails.net/2008/7/21/ruby-fringe" rel="alternate" type="text/html"/>
    <title>Ruby Fringe</title>
<content type="html">
            &lt;p&gt;&lt;a href='http://rubyfringe.com'&gt;Ruby Fringe&lt;/a&gt; wrapped up today and it was incredible. All of the presentations were compelling, the food was great and the parties were tons of fun. I met a ton of interesting people (who are way smarter than me), and find myself exhausted and happy.&lt;/p&gt;


	&lt;p&gt;Congratulations goes out to Pete, Meghann and the rest of the &lt;a href='http://unspace.ca'&gt;Unspace&lt;/a&gt; for pulling together such a great event.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-06-25:41</id>
    <published>2008-06-25T16:19:00Z</published>
    <updated>2008-12-31T15:32:55Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/6/25/upgrading-non-trivial-apps-to-rails-2-1" rel="alternate" type="text/html"/>
    <title>Upgrading non-trivial apps to Rails 2.1</title>
<content type="html">
            &lt;p&gt;I finished updating Learnhub to run on Rails 2.1. I wrote an article about a few things I learned along the way:
&lt;a href='http://rails.learnhub.com/lesson/page/2433-upgrading-non-trivial-apps-to-rails-21'&gt;Upgrading non-trivial apps to Rails 2.1&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-06-12:40</id>
    <published>2008-06-12T15:50:00Z</published>
    <updated>2008-12-31T15:33:03Z</updated>
    <category term="Archive"/>
    <link href="http://blog.viarails.net/2008/6/12/microsoft-office-protocol-discovery-is-annoying" rel="alternate" type="text/html"/>
    <title>Microsoft Office Protocol Discovery is annoying</title>
<content type="html">
            &lt;p&gt;If you&#8217;re getting annoying exception notifications, generated by a client named &#8220;Microsoft Office Protocol Discovery&#8221;, or even &#8220;Microsoft Data Access Internet Publishing Provider Protocol Discovery&#8221; this will help.&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://rails.learnhub.com/lesson/page/2329-dealing-with-microsoft-office-protocol-discovery-in-rails'&gt;http://rails.learnhub.com/lesson/page/2329-dealing-with-microsoft-office-protocol-discovery-in-railsv&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-06-11:39</id>
    <published>2008-06-11T17:15:00Z</published>
    <updated>2008-12-31T15:33:09Z</updated>
    <category term="Archive"/>
    <category term="learnhub"/>
    <link href="http://blog.viarails.net/2008/6/11/fun-with-learnhub" rel="alternate" type="text/html"/>
    <title>Fun with Learnhub</title>
<content type="html">
            &lt;p&gt;We finally have launched the &lt;a href='http://learnhub.com'&gt;new Learnhub homepage&lt;/a&gt; after spending a lot of effort on it. It looks great!&lt;/p&gt;


	&lt;p&gt;&lt;a href='http://blog.viarails.net/assets/2008/6/11/homepage-big.png'&gt;&lt;img src='http://blog.viarails.net/assets/2008/6/11/homepage-small.png' alt='' /&gt;&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;Also, &lt;a href='http://techcrunch.com'&gt;TechCrunch&lt;/a&gt; posted a &lt;a href='http://www.techcrunch.com/2008/06/11/learnhub-relaunches-its-social-learning-network/'&gt;positive article on Learnhub&lt;/a&gt;
Check it out!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-06-04:38</id>
    <published>2008-06-04T01:55:00Z</published>
    <updated>2008-12-31T15:33:17Z</updated>
    <category term="Archive"/>
    <category term="railsconf08"/>
    <link href="http://blog.viarails.net/2008/6/4/railsconf-08-on-video" rel="alternate" type="text/html"/>
    <title>Railsconf 08 on video</title>
<content type="html">
            &lt;p&gt;There were some great interviews recorded at Railsconf, and &lt;a href='http://railsenvy.com'&gt;Rails Envy&lt;/a&gt; release some new videos. Here they are:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;a href='http://rails.learnhub.com/lesson/page/2088-mvc-public-service-announcements'&gt;&lt;span class='caps'&gt;MVC&lt;/span&gt; Public Service Announcements&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href='http://rails.learnhub.com/lesson/page/2090-railsconf-developer-interviews'&gt;Railsconf developer interviews&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href='http://rails.learnhub.com/lesson/page/2089-railsconf2008-in36-minutes'&gt;Railsconf in 36 minutes&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;
          </content>  </entry>
  <entry xml:base="http://blog.viarails.net/">
    <author>
      <name>Wesley</name>
    </author>
    <id>tag:blog.viarails.net,2008-05-21:37</id>
    <published>2008-05-21T16:38:00Z</published>
    <updated>2008-12-31T15:33:31Z</updated>
    <category term="Archive"/>
    <category term="rails session store"/>
    <link href="http://blog.viarails.net/2008/5/21/session-store-switching" rel="alternate" type="text/html"/>
    <title>Session Store Switching</title>
<content type="html">
            &lt;p&gt;I wrote a short article about a little gotcha, and the solution to it when switching your rails session store. Check it out: &lt;a href='http://rails.learnhub.com/lesson/page/1858-how-to-switch-your-default-session-store'&gt;http://rails.learnhub.com/lesson/page/1858-how-to-switch-your-default-session-store&lt;/a&gt;&lt;/p&gt;
          </content>  </entry>
</feed>
