Close
Glad You're Ready. Let's Get Started!

Let us know how we can contact you.

Thank you!

We'll respond shortly.

PIVOTAL LABS
Standup 2010.08.06: Evil Scopiness Edition

Interesting Things

find_in_batches has Evil Scopiness

ActiveRecord::Base#find_in_batches() (and therefore #find_each()) is applying a scope around the block it is given, which applies to any subsequent find call on that model:

Let’s say we have a Comment model that act_as_tree so comments can be threaded. In the following code:


Comment.find_each(:conditions => "comments.state = 'published'") do |comment|
puts comment.parent.content
end

The association call to .parent will also have the scope {:conditions => "comments.state = 'published'"} applied to it. This can have highly unexpected side-effects.

The way to get around this is:


Comment.find_each(:conditions => "comments.state = 'published'") do |comment|
Comment.with_exclusive_scope(:find => {}) do
puts comment.parent.content
end
end

Easy Ruby profiling

If you’re profiling Ruby code often, here’s a code snippet that can be useful:

Comments
  1. Alex Chaffee says:

    `STDOUT` always points to the file handle that stdout had when ruby was started. Use `$stdout` instead, which allows people to redirect standard output at runtime, which is very useful for e.g. tests, or running a server inside a server.

    More details at < http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout> and < http://www.ruby-forum.com/topic/178044#779619>.

Post a Comment

Your Information (Name required. Email address will not be displayed with comment.)

* Copy This Password *

* Type Or Paste Password Here *