Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Tools
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 2010.08.06: Evil Scopiness Edition

Davis W. Frank
Friday, August 6, 2010

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:

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 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>.

    August 8, 2010 at 5:51 pm

  2. Alex Chaffee says:

    For example, I use this function a lot in tests, either to assert that a method outputs something, or just to suppress it: < http://gist.github.com/514760>

    Of course, since you are outputting profile information, you may want it to always go to the console (original `STDOUT`), no matter what the value of `$stdout`, even in a test. In which case, nevermind :-)

    August 8, 2010 at 5:58 pm

Add New Comment Cancel reply

Your email address will not be published.

Davis W. Frank

Davis W. Frank
San Francisco

Recent Posts

  • An Entirely Late Movember Wrap-Up
  • Mid-ish Movember
  • Getting Involved with Movember
Subscribe to Davis W.'s Feed

Author Topics

movember (2)
rails (16)
palm (6)
agile (14)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Tools
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >