Nested include has major memory leak (Rails 2.0.1).

Posted by Bart ten Brinke Wed, 02 Jul 2008 14:00:33 GMT

As our mongrels were using up quite a lot of memory, so I tried to figure out what was causing this.

When running the app locally I found out that one certain page caused the mongrel to grow from 60 to 190 megabytes. A whopping 130 megabytes!

After commenting out some of the code, I realized that a single line was causing all of the memory usage

contracts = Contract.find( :all, 
  :conditions => ['contracts.employee_id IN (?) ', employees ],
  :include => [:expertise_profile => :qualifications ] )

Ouch! The nested include of rails somehow leaks a large amount of memory. The fix was a piece of cake.

contracts = Contract.find( :all, 
  :conditions => ['contracts.employee_id IN (?) ', employees ],
  :include => :expertise_profile )

Now my mongrel stays at 60 megabytes. I don't know if this issue persists in the new 2.1 Rails, but I'll check that soon!

Posted in  | Tags , , , ,  | 4 comments

Testing your Application Controller with rSpec

Posted by Bart ten Brinke Wed, 23 Jan 2008 13:35:15 GMT

I was trying to create a function that would check the enforcement of the before filters in my application controller. After going through a lot of rspec documentation and examples, I found nothing that really suited my needs. After a long google, I found a mention in the rspec mailing list and a hint to a solution for this. This is a working example of this idea.

I have in my application controller the following code:

class ApplicationController < ActionController::Base
  before_filter :check_authorization
   ....
  # Checks if a user is authorized
  def check_authorization
    User.check_authorization(controller_name, action_name)
  end
end

And in spec/controllers/application/application_spec.rb the following description.

describe "an authorized controller", :shared => true do

  it "should have the check_authorization set in the before filter" do
    ApplicationController.before_filters.should \
      include(:check_authorization)
  end   
end

In all other controller specs I can now do this:

require 'application_controller_spec'

describe UnitsController, "in general" do
  it_should_behave_like "an authorized controller"
end

By doing this I now have a spec that ensures that every controller checks authorizations before doing anything else. How cool is that :)!

It would be nicer if the test handled skip before_filters in some nice way like: it_should_behave_like "a monkey".except_for_action('show'). Has anyone got any ideas for that?

Special thanks to Matthijs Langenberg for his insights.

Posted in  | Tags , ,  | 13 comments

Moving to 2.0

Posted by Andre Foeken Thu, 18 Oct 2007 13:27:19 GMT

Issues found so far:

  • Lots of small routing changes. Thank god for grep! : addresses_path(@employee) => employee_addresses_path(@employee) . (Don't forget the *_url methods!)

  • Several plugins failed due to extract_options_from_args!. This method has been replaced with the nicer: args.extract_options! (i.e acts_as_paranoid, acts_as_mappable, paginating_find)

  • acts_as_paranoid had minor issues: fix by replacing construct_count_with_legacy_args to construct_count_options_from_args

  • ActiveResource is now part of rails core. Be sure to freeze edge twice if you are upgrading from 1.2.3 or lower. All of our libXML additions had to be redone. (this time through /patches, tnx fngtps)

  • Different behavior of the render method. Before you could call render "addresses/show" if you wanted to, this has been changed to render :template => "addresses/show". This affects several plugins too (like rspec_on_rails)

  • We no longer need the mysql_tasks plugin, since this functionality is now build right in!

  • redhillonrails_core has some issues with connection adapters. Apparently rails 2.0 no longer loads adapters it doesn't need. This creates some issues with the redhills plugin since it tries to include stuff in those adapters. Adding a begin/rescue block around each include solves the issue.

  • The development environment no longer needs the config.breakpoint_server = true setting.

  • Polymorphic models are now saved with the base class as type in external objects.

  • Autocomplete textfields are now a plugin: ./script/plugin install auto_complete

  • If you have overridden the to_json methods, be sure to change that to to_json options={}, else they might fail.

  • Migrations no longer working? Try removing duplicate names. We have two migrations with the same name (but different id) and it just skipped the first one (!)

Benefits so far:

  • Speed! We did expect some speed increase, but this is major! Our pdf generating stuff uses a lot of ActiveRecord and we saw decreases of more than 50% request time.

Posted in ,  | Tags , , , ,  | 2 comments

Rails Security

Posted by Bart ten Brinke Thu, 20 Sep 2007 17:30:54 GMT



After a security presentation at RailsConfEurope 2007, I found a lot was missing, so I made this. I didn’t finish it in time for reject conf, so I posted it here. Now it's time for me to go on vacation! See you next week!

Posted in  | Tags , , , , ,  | 8 comments

Gettext Generators 1.2.3 Released

Posted by Bart ten Brinke Tue, 28 Aug 2007 18:43:07 GMT

The longawaited update for the Gettext Generator.

  • First release as plugin
  • Added svn:merge raketask
  • Updated generated resource to mimmick scaffold_generator packaged with rails 1.2.3 tag

So go to RubyForge or take a look here.

I'm currently getting the plugin uploaded to rubyforge. As soon as I have that figured out how that works, I'll post the link here.

ruby script/plugin install svn://rubyforge.org/var/svn/gettextgnrtrs/tags\ /Gettext-Generators-1.2.3/

Posted in , ,  | Tags , ,  | no comments