<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>paulsturgess.co.uk articles</title>
    <link>http://www.paulsturgess.co.uk/archive</link>
    <description>paulsturgess.co.uk Ruby on Rails articles</description>
    <item>
      <title>Using rspec to test a named_scope in Ruby on Rails</title>
      <link>http://www.paulsturgess.co.uk/articles/show/93-using-rspec-to-test-a-named_scope-in-ruby-on-rails</link>
      <description>&lt;p&gt;When testing a named_scope it's important to test the expected behaviour of the method, not how it's implemented, as this will allow you to re-factor your code using the test as an indicator that the re-factor was successful.&lt;/p&gt;

&lt;p&gt;So in your model you might have...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class User &lt; ActiveRecord::Base
    named_scope :active, :conditions =&gt; [&quot;active = ?&quot;, true]
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In your model spec I'd then have...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;describe User, &quot;::active&quot; do
    it &quot;includes users that are active&quot; do
        @user = Factory(:user, :active =&gt; true)
        User.active.should include(@user)
    end

    it &quot;excludes users that are not active&quot; do
        @user = Factory(:user, :active =&gt; false)
        User.active.should_not include(@user)
    end
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The key thing here is that we're not actually calling &lt;code&gt;named_scope&lt;/code&gt;, we could change the method to be a regular class method if we wanted and the test would still pass.&lt;/p&gt;

&lt;p&gt;It may be a good idea to check that the model has the method defined like so...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;User.should respond_to(:active)&lt;/code&gt;&lt;/pre&gt;

</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/93-using-rspec-to-test-a-named_scope-in-ruby-on-rails</guid>
    </item>
    <item>
      <title>Hosting a ruby on rails blog on Heroku cloud hosting</title>
      <link>http://www.paulsturgess.co.uk/articles/show/92-hosting-a-ruby-on-rails-blog-on-heroku-cloud-hosting</link>
      <description>&lt;p&gt;I've taken the plunge and moved my site over to Heroku ruby cloud host. My initial impressions of the service are great so I thought I'd post up my experience so far.&lt;/p&gt;

&lt;p&gt;I've opted for the basic free setup to see how it goes. It's difficult to compare what you get to other hosts because Heroku is so different. Everything is hosted in the cloud on Amazon EC2, you create a 'compiled slug' which is a read only version of your app and that's hosted on the &lt;a href=&quot;http://heroku.com/how/dyno_grid&quot;&gt;'Dyno Grid'&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have one &lt;a href=&quot;http://heroku.com/how/dynos&quot;&gt;'Dyno'&lt;/a&gt; which is kind of equivalent to a single Mongrel, essentially the more Dyno's the more concurrent your app. I figure that as my blog is so simple that concurrency is a non issue, anyway if it became one I can easily add more Dynos.&lt;/p&gt;

&lt;p&gt;Dynos are actually spread across multiple servers so I need to &lt;a href=&quot;http://heroku.com/how/dyno_grid#2&quot;&gt;forget servers&lt;/a&gt;. The main difference is that I don't have ssh access and I can't write to anywhere. This means user uploads (not that I have any) would need to be stored somewhere like &lt;a href=&quot;http://aws.amazon.com/s3/&quot;&gt;Amazon S3&lt;/a&gt;. Plugins like &lt;a href=&quot;http://github.com/thoughtbot/paperclip&quot;&gt;Paperclip&lt;/a&gt; make this so easy anyway that it's pretty much a non-issue.&lt;/p&gt;

&lt;p&gt;The other main change is the pure git work-flow. That's right no &lt;a href=&quot;http://www.capify.org/index.php/Capistrano&quot;&gt;Capistrano&lt;/a&gt;. I couldn't believe how easy it is to deploy to Heroku though, all you do is push code to your repository and it's live.&lt;/p&gt;

&lt;p&gt;But what about database migrations? Well as far as I can tell you need to pull down your database, make any changes locally and then push it back up to Heroku. I guess you would need to temporarily take down the site between updating the code and the database. I need to investigate further here but for my blog I can't see it being a problem.&lt;/p&gt;

&lt;p&gt;Heroku provide a lot of &lt;a href=&quot;http://addons.heroku.com/&quot;&gt;addons&lt;/a&gt; to extend your app. I'm making full use of the free 5mb of memcache and while that doesn't sound like much everything appears to be running so much faster, I can always upgrade for a fee if I want.&lt;/p&gt;

&lt;p&gt;Moving my domain couldn't have been easier because of the way Heroku have integrated with &lt;a href=&quot;http://www.zerigo.com/&quot;&gt;Zerigo&lt;/a&gt; for DNS, no sign up required at all.&lt;/p&gt;

&lt;p&gt;I'm also using another addon for sending email through &lt;a href=&quot;http://sendgrid.com/&quot;&gt;SendGrid&lt;/a&gt;. I have this just for notifications of new comments or exceptions. I can send upto 200 emails per day for free.&lt;/p&gt;

&lt;p&gt;So far everything has gone so smoothly, fingers crossed it stays that way as I'm loving the zero hosting costs!&lt;/p&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/92-hosting-a-ruby-on-rails-blog-on-heroku-cloud-hosting</guid>
    </item>
    <item>
      <title>How to install a Ruby on Rails plugin from a specific git repository branch</title>
      <link>http://www.paulsturgess.co.uk/articles/show/91-how-to-install-a-ruby-on-rails-plugin-from-a-specific-git-repository-branch</link>
      <description>&lt;p&gt;Lots of Ruby on Rails plugins are being updated for Rails 3 and thus for older Rails apps you don't always want to install the latest and greatest version.&lt;/p&gt;

&lt;p&gt;Recently I needed to install the Rails 2.3 stable version of the  exception notification plugin and so I needed to specify a particular branch in the git repository.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;script/plugin install git://github.com/rails/exception_notification.git -r 2-3-stable&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;-r&lt;/code&gt; option allows you to identifiy the specific branch you're after.&lt;/p&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/91-how-to-install-a-ruby-on-rails-plugin-from-a-specific-git-repository-branch</guid>
    </item>
    <item>
      <title>Using named scopes (named_scope) in Ruby on Rails</title>
      <link>http://www.paulsturgess.co.uk/articles/show/90-using-named-scopes-named_scope-in-ruby-on-rails</link>
      <description>&lt;p&gt;Ruby on Rails 2.1 introduced the notion of a Named Scope. For me it's easily been one of the most beneficial features in helping to keep my code 'DRY'.&lt;/p&gt; 

&lt;p&gt;Essentially it allows you to apply conditions that you want to use when finding records without having to write out those conditions each time.&lt;/p&gt;

&lt;p&gt;For example, I have an Article model and one of the attributes for an article is 'published'. Now in the old days I might have written code in my controller that looked something like this...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;@articles = Article.find(:all, :conditions =&gt; [&quot;published = ?&quot;, true])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I would have repeated this every time I wanted to get all the published articles out. The problem with this approach being that if what determines if an article is 'published' changes, then I'd need to go and change it in x number of places I'd written that line.&lt;/p&gt;

&lt;p&gt;Introducing &lt;code&gt;named_scope&lt;/code&gt;. In the Article model I simply define a named scope with a name and then my conditions...&lt;/p&gt; 

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Article &lt; ActiveRecord::Base
  named_scope :published, :conditions =&gt; [&quot;published = ?&quot;, true]
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now in my controllers I can use...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;@articles = Article.published&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So much better, it's clearer, it's quicker to type and I can now update that published method in one place, safe in the knowledge it will ripple throughout all my controllers.&lt;/p&gt;

&lt;p&gt;I can also chain named scopes together...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Article &lt; ActiveRecord::Base
  named_scope :published, :conditions =&gt; [&quot;published = ?&quot;, true]
  named_scope :has_comments, :conditions =&gt; [&quot;comments_count &gt; 0&quot;]
end

@articles_with_comments = Article.published.with_comments&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can also pass in variables to named scopes by using a lambda...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Article &lt; ActiveRecord::Base
  named_scope :by_author, lambda { |author| { :conditions =&gt; [&quot;author_id = ?&quot;, author.id] }}
end

@your_articles = Article.published.by_author(current_user)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That last example is probably unrealistic as it's more likely you would call it like so...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;@articles = @author.articles.published&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that you can still use the named scope even though you're  grabbing the articles via an Author model instance.&lt;/p&gt;

&lt;p&gt;You can also use named scopes to setup commonly used limits or orders.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;class Article &lt; ActiveRecord::Base
  named_scope :recent_limit, :limit =&gt; &quot;5&quot;
  named_scope :order_by_last_comment, :order =&gt; &quot;last_comment_at DESC&quot;
end&lt;/code&gt;&lt;/pre&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/90-using-named-scopes-named_scope-in-ruby-on-rails</guid>
    </item>
    <item>
      <title>A Ruby on Rails will_paginate gotcha</title>
      <link>http://www.paulsturgess.co.uk/articles/show/89-a-ruby-on-rails-will_paginate-gotcha</link>
      <description>&lt;p&gt;Here's a cheeky gotcha to keep an eye out for when using the popular pagination plugin/gem for Ruby on Rails &lt;a href=&quot;http://gemcutter.org/gems/will_paginate&quot;&gt;will_paginate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was doing something like...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.find(:all, :order =&gt; &quot;bar DESC&quot;).paginate :page =&gt; params[:page]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Looks harmless enough. However, what I didn't realise is that the paginate method cannot run efficiently with the &lt;code&gt;find(:all, ...)&lt;/code&gt; method in there. &lt;/p&gt;

&lt;p&gt;The whole point of the pagination is to only load the objects you need to for the page, an accompanying sql count allows it to build the links to additional pages.&lt;/p&gt;

&lt;p&gt;However, the code above will actually instantiate every single Foo object and load it into memory; nasty.&lt;/p&gt;

&lt;p&gt;I tried removing the order to see if that was the problem...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.all.paginate :page =&gt; params[:page]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But still it remained inefficient.&lt;/p&gt;

&lt;p&gt;The fix was to remove the &lt;code&gt;all&lt;/code&gt; method and use the built in paginate :order paramater like so...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.paginate :page =&gt; params[:page], :order =&gt; &quot;bar DESC&quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now it loads only those objects required for the page.&lt;/p&gt;

&lt;p&gt;For a moment I feared how the paginate method had been dealing with named scopes but I can confirm it is happy to work with those and retain its' efficiency, only loading what is required for the page.&lt;/p&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/89-a-ruby-on-rails-will_paginate-gotcha</guid>
    </item>
    <item>
      <title>How to efficiently perform a bulk update of lots of records in Ruby on Rails</title>
      <link>http://www.paulsturgess.co.uk/articles/show/88-how-to-efficiently-perform-a-bulk-update-of-lots-of-records-in-ruby-on-rails</link>
      <description>&lt;p&gt;If you are looking to update a lot of database records at once in Ruby on Rails then it's worth considering the different ways it can be done and which is the fastest and most efficient.&lt;/p&gt;

&lt;p&gt;One way is to loop/iterate round a collection of objects and update each one in turn. Your code might look something like...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.all.each do |foo|
   foo.update_attributes(:bar =&gt; true)
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, it's worth noting that using a block in that way will instantiate each Foo object into memory and will also generate a mysql query per record. The advantage of this is that any Active Record callbacks will be triggered and each object will be validated.&lt;/p&gt;

&lt;p&gt;Another, faster, way to do this generating just a single SQL UPDATE statement would be to use the &lt;code&gt;update_all&lt;/code&gt; method like so...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.update_all(:bar =&gt; true)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It does not instantiate the involved records and the single sql query makes this method faster and more efficient. However, it will not trigger any Active Record callbacks such as 'after_save' etc. so if you need these you'll have to go with the first method of updating each object in turn.&lt;/p&gt;

&lt;p&gt;Note that the &lt;code&gt;update_all&lt;/code&gt; method can accept conditions as second argument in the same format as the rails find method. &lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;Foo.update_all({:bar =&gt; true}, [&quot;id IN (?)&quot;, foo_ids])&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Alternatively you can call update_all on named scopes.&lt;/p&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/88-how-to-efficiently-perform-a-bulk-update-of-lots-of-records-in-ruby-on-rails</guid>
    </item>
    <item>
      <title>How to freeze gems in Ruby on Rails</title>
      <link>http://www.paulsturgess.co.uk/articles/show/87-how-to-freeze-gems-in-ruby-on-rails</link>
      <description>&lt;p&gt;It's always a good idea to freeze your gems in your Ruby on Rails app when you don't have full control over your hosting environment.&lt;/p&gt;

&lt;p&gt;To freeze any gems that are listed inside your &lt;code&gt;Rails::Initializer&lt;/code&gt; config block just run the following on the command line...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;rake gems:unpack&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Your gems will be frozen in the vendor/gems directory.&lt;/p&gt;

&lt;p&gt;Alternatively you can freeze a specific gem version by...&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[~/yourapp/vendor/gems] gem unpack gem_name -v=&amp;lt;version&amp;gt;&lt;/code&gt;&lt;/pre&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/87-how-to-freeze-gems-in-ruby-on-rails</guid>
    </item>
    <item>
      <title>Missing these required gems: RedCloth - config.gem Ruby on Rails error</title>
      <link>http://www.paulsturgess.co.uk/articles/show/86-missing-these-required-gems-redcloth---configgem-ruby-on-rails-error</link>
      <description>&lt;p&gt;RedCloth is one of those gems that is not consistently named and has &lt;a href=&quot;http://paulsturgess.co.uk/articles/show/77-actionviewtemplateerror-uninitialized-constant-erractstextiledclassmethodsredcloth&quot;&gt;caused me problems in the past&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This time even though I had specified it's requirement in my environment.rb file inside the &lt;code&gt;Rails::Initializer&lt;/code&gt; block I was still getting the 'Missing these required gems' error.&lt;/p&gt;

&lt;p&gt;I had frozen the gem into the vendor directory so obviously it wasn't being found by Rails.&lt;/p&gt;

&lt;p&gt;Turns out all I needed to do was add a lib parameter to the config.gem line...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;config.gem 'RedCloth', :version =&gt; '&gt;=x.x.x', :lib =&gt; 'redcloth'&lt;/code&gt;&lt;/pre&gt;
</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/86-missing-these-required-gems-redcloth---configgem-ruby-on-rails-error</guid>
    </item>
    <item>
      <title>Ruby on Rails sort_by vs sort</title>
      <link>http://www.paulsturgess.co.uk/articles/show/85-ruby-on-rails-sort_by-vs-sort</link>
      <description>&lt;p&gt;Given the choice when sorting arrays in Ruby it's typically better to use &lt;code&gt;sort_by&lt;/code&gt; rather than &lt;code&gt;sort&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort_by&lt;/code&gt; uses the &lt;a href=&quot;http://en.wikipedia.org/wiki/Schwartzian_transform&quot;&gt;Schwartzian transform&lt;/a&gt; technique that is a Perl programming idiom used to improve the efficiency of sorting a list of items.&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;sort_by&lt;/code&gt; like so...&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;foo.sort_by(&amp;:bar)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The equivalent using &lt;code&gt;sort&lt;/code&gt; would look like...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;foo.sort{|a,b| b.bar &lt;=&gt; a.bar}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, using sort will mean that the method bar is called twice for each comparison and therein lies the inefficiency.&lt;/p&gt;</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/85-ruby-on-rails-sort_by-vs-sort</guid>
    </item>
    <item>
      <title>How to include class and validation methods using a module in Ruby On Rails</title>
      <link>http://www.paulsturgess.co.uk/articles/show/84-how-to-include-class-and-validation-methods-using-a-module-in-ruby-on-rails</link>
      <description>&lt;p&gt;A great way to re-use code across your Ruby on Rails application is to use a module and include the methods contained within it where you need them.&lt;/p&gt;

&lt;p&gt;You simply create a file (typically named the same as your module) inside the lib directory where it is automatically loaded by Rails. For example a file lib/foo.rb may look like this...&lt;/p&gt; 

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;module Foo

  def bar
    # ...
  end

end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now in a class you can make the &lt;code&gt;bar&lt;/code&gt; method available as an instance method thus:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;include Foo&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;However, if you want to include a class method you need to modify the class that is including the module, this can be done using the &lt;code&gt;included&lt;/code&gt; method. Most plugins use the &lt;code&gt;included&lt;/code&gt; method as follows...&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;module Foo

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def my_class_method
      # ...
    end
  end

  def bar
    # ...
  end

end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You simply put all your class methods inside the &lt;code&gt;ClassMethods&lt;/code&gt; module (omitting the usual &lt;code&gt;self&lt;/code&gt; part of the name).&lt;/p&gt;

&lt;p&gt;However, this doesn't work for all class methods. For example validation methods and callbacks are typically called without the &lt;code&gt;self&lt;/code&gt; prefix as it is implicit from the class that is defining the method. When including these methods you need to explicitly set the class, this can be achieved thus:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;module Foo

  def self.included(base)
    base.validates_presence_of :bar
    base.before_save :some_method
  end

end&lt;/code&gt;&lt;/pre&gt;

</description>
      <guid>http://www.paulsturgess.co.uk/articles/show/84-how-to-include-class-and-validation-methods-using-a-module-in-ruby-on-rails</guid>
    </item>
  </channel>
</rss>
