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
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker
Adam Milligan

#method_missing makes me eat my words

Adam Milligan
Saturday, December 27, 2008

A while back I wrote about private methods in ActiveRecord objects, and how Rails 2.2 makes them behave as they should. ActiveRecord associations will no longer respond to private methods defined on their targets; however, my colleague Joseph pointed out that they also no longer respond to methods defined via #method_missing on their targets. Which sucks horse poop through a straw, to some extent.

In my original post I wrote this, fully aware that I was writing lies, but mostly in a rush to get on to my point at the time:

Sometimes I’m forced to use #send:

method_name = extract_method_name_from_the_aether
some_object.send(method_name)

In order to make this code correct with regard to access control I have to add cruft:

method_name = extract_method_name_from_the_aether
some_object.send(method_name) if some_object.respond_to?(method_name)

The astute reader will note that the second code block doesn’t actually approximate the behavior of calling a private method via function call syntax. It should look more like this:

method_name = extract_method_name_from_the_aether
raise NoMethodError if some_object.private_methods.include?(method_name)
some_object.send(method_name)

Two things to notice here: first, the original code didn’t throw an exception if it failed to call the method; second, and much more importantly, the set of private methods is not the complement of the set of methods an object will respond to. So, using !#respond_to? as the condition for preventing the method call is too restrictive. The fact that this code will prevent calling a non-private method defined via #method_missing clearly shows this.

Now, I know the Rails core team chose to use #respond_to? in this particular case for a good reason (which my original patch did not take into consideration): performance. Some simple investigation quickly shows that #include? is an order of magnitude slower than #respond_to? Method calls happen quite a lot, so slow is bad.

I’ve submitted another patch that should correct the #method_missing behavior without dragging everything to a halt. Building on my previous examples, the code looks something like this:

method_name = extract_method_name_from_the_aether
raise NoMethodError if !some_object.respond_to?(method_name) && some_object.private_methods.include?(method_name)
some_object.send(method_name)

Note that the conditional expression is now redundant; the first condition cannot be false if the second condition is true. However, an object will likely respond to the majority of method calls sent to it. The first conditions will (quickly) evaluate to false in these cases, short-circuiting the conditional. In the minority of cases where the first condition evaluates to true, the second condition will (slowly, but correctly) determine if the method call violates access control.

Unfortunately, sensible as it may seem, this change actually breaks has_one :through associations, because of collection-specific functionality they inherit from has_many :through associations. (Oh yes, has_one :through associations are collections, based on their place in the ActiveRecord inheritance hierarchy. Or, they were; read more about that here).

And, finally, I think it’s important to note that none of these machinations in Rails would be necessary if #send (and #send!) actually worked as it should. Heads up, Matz.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Edward Hieatt

Back in the land of the living (or: How RubyMine makes me happy)

Edward Hieatt
Wednesday, December 17, 2008

How my outlook on coding in Rails has changed over the past few months!

When I made the switch from Java to Rails a few years back, I, like many of my fellow Pivots making that same well-chronicled transition, delighted in the ease with which we could suddenly knock out a web app. How we cheered when our object-relational mapping took zero lines of code! How we applauded when we declared our model object validations in near-English! How we roared with laughter when convention viciously slapped the face of configuration! And how we shook our heads in dismay when we realized that our new development environment appeared to be from the mid-, if not early, nineties.

For, while we had arrived in a brave new world of minimalist declarative meta-programming, rapid prototyping, and an new-found sense of productivity that made even the most nimble forms of Java development look like wading through a morass of slimy boilerplate code and endless XML, we soon realized that the IDE situation was less than awesome. Our productivity was overall much improved, but we had taken a huge step backwards when it came to the act of writing – and especially changing – code and tests. Overnight, we went from living it up in a paradise of automated refactorings, seamlessly inbuilt test runners and powerful debuggers to roughing it with a text editor that, to our spoilt eyes, appeared to offer barely more than code highlighting and support for homemade macros.

Not only didn’t our favorite Java IDE, IntelliJ, not function well with Ruby, but nor did Eclipse, and nor did NetBeans. Early on, each had some nominal support for Ruby, it’s true, but it was mostly just code highlighting and some basic navigation. If we wanted to run a test, we had to (horror of horrors) leave our IDE, go to a shell and run a command (mapping a key to an “external tool” was cold comfort, it seemed to me). If we wanted to rename a variable, we had to do it manually. And if we wanted to debug something, well, the only option was to use a tool that appeared so prehistoric that we simply didn’t do it.

In fact, things were so bad that if we were honest with ourselves, TextMate sometimes looked like a better option that the IDE we knew and loved. We pleaded with Pivots who didn’t come from the world of Java to use IntelliJ, if only (for them) for its awesome global search, and if only (for us) to make ourselves feel comfortable in the new world of Rails. But IntelliJ had been rendered so impotent by its Ruby Kryptonite that it felt at times as if we were simply dragging around a comfort pillow that had lost its stuffing. All the progress that had been made in the world of Java IDEs for so many years seemed to have been lost. Woe was certainly us.

But then came some signs of life. NetBeans and Eclipse (in its various forms) were making progress in Ruby-land: they had test runner integration and debuggers that worked. Big steps forward indeed. While I suspected that I would probably miss IntelliJ, I nevertheless gallantly tried to commit to one of them: if they were going to offer me something even close to the power I used to have in my Java IDE, I was willing to put up with a lot. But despite my best attempts to be patient, I was disappointed time and time again. Eclipse still felt clunky compared to IntelliJ. NetBeans still suffered from a problematic global search and less-than-perfect VCS integration. They are gallant attempts, and they deserve credit. They’re both great products and they both serve communities that no doubt find them invaluable. But in that very unquantifiable, personal, emotional way, they weren’t what I really wanted. I wanted that IntelliJ feel.

So where was JetBrains on this? There was the Ruby plugin for IntelliJ, which was making some great strides forward, but that always felt like a bolt-on solution at best. What was more, it often didn’t work with new IntelliJ updates.

Imagine my surprise and delight, then, when Christmas came several months early this year. Suddenly, in late 2008, we are quietly presented with RubyMine. IntelliJ for Ruby? Surely not! I dared not hope for too much when with some trepidation I downloaded an early pre-EAP candidate a few months ago. But, joy of joys, it turned out that it was, or rather, is gradually becoming, (almost) all I was hoping for. Built-in test runner, debugger, pretty good refactorings – all things that other IDEs provide, but in that special JetBrains way that is so much more intuitive. Great success!

Now, I realize that all this probably sounds like a gush of praise that is not yet due, and perhaps that’s true. It’s certainly true that there’s a long way to go: RubyMine is lacking in many ways. But the signs are there that things are moving hastily in the right direction. Recently, each build that has come out has been noticeably better to me than the last (additional useful features, stability, performance). But more importantly, I feel like JetBrains is on the right trajectory with RubyMine. We now have a path towards a great IDE for Rails. I think we’re back in the land of the living, and I’m hopeful that between Rails and RubyMine our productivity will soon jump higher than we could have imagined just a few years ago.

OK, start the “All you need is Emacs”, “VIM is the best thing ever”, and “TextMate rocks, what’s this IDE nonsense?” comments below :)

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Davis W. Frank

Standup 12/5/2008: RubyMine gets Focused; We're slow & lazy

Davis W. Frank
Friday, December 5, 2008

Interesting Things

  • A polite reminder that when defining associations that need :class_name or :source keys that the values should be strings and not Ruby constants. That way lies madness.
  • RubyMine build 500 gets running “Focused” Test::Unit tests (file or single test) right via Ctrl-Shift-F10. Specs aren’t working quite right yet, but JetBrains appears to be working on it.

Ask for Help

“Anyone having odd slowness with Google Gears?”

We are having an issue with slowness in a Google Gears app, where certain actions take several times longer on some boxes. It is only slow on some of the client’s windows XP machines, but not all. Never on mac. It is slow in BOTH Firefox 3 and IE 7 on the slow machines.

The Gears sqlite db and dir does not seem to be the problem either – a clean IE data, cache and gears data dir still runs slow, and moving the ‘slow’ gears data dir to a fast machine runs fast.

Also cleared all IE data, compared/upgraded Gears versions, Java versions, and Windows Service Pack versions. Nothing seems to be a common denominator yet. Nothing obvious on google searches…

“Multi-select widget for a ‘has_many :through’ relationship?”

I have an association like this:

class Foo
  has_many :bars, :through => :barrings
end

I’m looking for a ‘widget’ for the Foos page that will let me assign/unassign multiple Bars at once. Looking at the select, select_tag, options_for_select helpers, etc, reminded me that the Rails built-in stuff is lacking. I was hoping that someone has already written a sweet multi-select widget that I could be lazy and use.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Mike Grafton

New York Standup 11/24/2008

Mike Grafton
Monday, November 24, 2008

Interesting

  • Rails 2.2.2 is released!

  • Even Rails 2.2.2 isn’t always threadsafe. I found this out by running a script with JRuby from the command line. The script loaded the Rails environment and then launched two threads that simply tried to resolve an ActiveRecord class constant. Fireworks (in the form of LoadError) ensued deep inside of const_missing. I’ll post the full example later today.

  • Tsearch2 is now built into Postgres (as of 8.3). This means you must remove the metadata from your tables, since Postgres now stores it in a separate place.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Standup 11/14/2008: XXL Mongrels and Non-Model Reports

Pivotal Labs
Saturday, November 15, 2008

Interesting Things

  • The iPhone SDK version 2.2 was released.
  • An interesting Selenium Object Pattern was discussed.

Ask for Help

“How big should a typical mongrel be? Ours is starting out at over 200 megs but is not leaking from that point.”

Everyone agreed that 50MB to 70MB is standard and anything over 100MB is considered pretty big. People suggested RubyProf for inspecting object counts and possibly tracking down the memory hogging code.

“What pattern does everyone use for non-model Reports with ActiveRecord? We are trying to create a report that counts a single model and groups by two associated models”

There was consensus around modeling a distinct report object and calling the referenced models. For example, FooReport and FooReportController fit nicely in a RESTful Rails world.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

Standup 11/11/2008: Firefoxen goodness

Pivotal Labs
Thursday, November 13, 2008

Interesting Things

  • In response to our ask for help, Ray Baxter answered us in code with a script called Firefoxen. “It’s a script to automatically configure multiple installations of Firefox so that they open with different profiles.” You can grab Firefoxen on GitHub. Thanks Ray!
  • IE7 sends odd Accept headers (*.* instead of an explicit text/html) which can cause undesired behavior in Rails. Someone suggested manually setting the format in a before_filter:

    params[:format] ||= 'text/html'

    This was discouraged because it can cause problems elsewhere. A better solution was to put the html format at the top of any `respond_to` blocks:

    def show
      respond_to.html # run by default when type cannot be determined
      respond_to.js
    end
    
  • The Ruby MySQL Gem version 2.7 leaks memory for very large queries. The solution is to remove the 2.7 gem and manually install version 2.8 from source. This library is no longer a gem and must be installed from the mysql-ruby-2.8 tarball.

Ask for Help

“As a followup to Firefox SSL certificate problems…”

It turns out that our server running nginx had an old version of OpenSSL installed. Upgrading OpenSSL solved the problem.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Pivotal Labs

New York Standup 9/30/2008

Pivotal Labs
Tuesday, September 30, 2008
  • What’s the best way to import a million records into a postgres database via ActiveRecord (which is needed to implement some application-specific logic)? We anticipate waiting a second (or so) between inserts to avoid slowing down the production database (which is under load, almost entirely reads). If there is any ActiveRecord feature which helps batch together inserts, noone knew about it. As for generally how long this will take (estimates range from 9 to 27 hours), and what the load on the production database will be, we planned on answering that with a trial run of a small number of these records.

  • We’re thinking of having capistrano deploy to two demo servers, one particularly aimed at showing to prospective users of our application, and the other mostly for story acceptance. The former would be hosted at a hosting company; the latter an internally run machine. Several people reported they have done this on their projects, and the problems were minor, mostly having to do with whether the deployed location (/u/apps/whatever or some such) is different on the two machines (the solution would be to use the capistrano variables, but tracking down all the places that need to do that could be an issue).

  • Erector tip of the day: in a Rails project, you can put a file (named edit.rb or edit.html.rb) in your view directory, and Rails/Erector will find the template implicitly (as it would for ERB, HAML, etc). It is not necessary to explicitly call render from your controller method.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
David Stevenson

Pivots patch rails: named_scope with the :joins can cause table aliasing issues

David Stevenson
Monday, September 29, 2008

In order to accomplish some advanced search functionality, we’ve added a lot of named_scopes to our User model. This seems like a good idea, and well within the intended use for named_scopes. Unfortunately, we ran into issues with our :joins. We have a separate User and Profile model, but our advanced search scopes often needed both to make decisions. So we had some scopes that look like this:

class User
  named_scope :verified {
    :conditions => {:email_verified => true}
  }

  named_scope :answered_questions {
    :join => "INNER JOIN profiles ON profiles.user_id = users.id " +
                 "INNER JOIN answers ON answers.profile_id = profiles.id"
  }

  named_scope :with_name { lambda { |name|
    :join => "INNER JOIN profiles ON profiles.user_id = users.id",
    :conditions => ["profiles.name LIKE ?", "%#{name}%"]
  } }
end

Using these named_scopes, we wanted to dynamically construct a finder that would return the results the user was interested, such as: User.verified or User.answered_questions or even User.verified.answered_questions.with_name('Joseph'). The last scope caused issues, unfortunately, with table aliasing. The query ended up joining in the profiles table twice, in exactly the same way without renaming the table, so mysql rejects the query.

The easiest solution to this problem was to use only the hash form for :join clauses, such as :join => :profile. Rails correctly merges multiple consecutive join scopes that use hashes. If you need to use string joins (such as a LEFT JOIN rather than an INNER JOIN) or put a condition directly on your join, then merging goes out the window and the hashed form is immediately converted to a string and all consecutive joins are “merged” by appending them together.

We started by manually aliasing our scopes, but in some cases we were concerned about the amount of duplicate data this was causing in our queries.

We thought about creating a dependency framework for named_scopes, such that you could have a single :profile scope that other scopes were dependent on and it would only ever get added once. This seemed really difficult because of the way the with_scopes are constructed by named_scopes, there was no good place to keep track of these dependencies, and it would still cause problems if you had a manual with_scope, or :join in your find.

Finally we decided that rails fundamentally lacked the capability to deal with duplicate joins, and that we should solve this problem. It seemed a good solution was to allow :join options to take an array of strings as follows:

  named_scope :answered_questions {
    :join => ["INNER JOIN profiles ON profiles.user_id = users.id",
                 "INNER JOIN answers ON answers.profile_id = profiles.id"]
  }

Now calling User.answered_questions.with_name('Joseph') will create three values in a :join array, two of which are identical and will be uniq’d out. The downside to this approach is that each value in the :join array has to be string identical, or it will not be properly uniq’d.

So if you are mixing hash style :profile joins with string joins of the same table you need to be careful you match the rails generated syntax. We mostly use string style joins to avoid this issue.

Here’s the ticket the we filed and patched:
1077-chaining-scopes-with-duplicate-joins-causes-alias-problem

It has been commited and will roll out with rails 2.2. Since then we have filed two more issues related to :join and :include:

  • 1078-using-include-assoc-and-join-assoc-leads-to-alias-issue
  • 1104-references_eager_loaded_tables-should-search-tables-in-join-clauses

We hope to patch these two as well!

Joseph & David

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Dan Podsedly

October 's NYC Ruby Happy Hour is next Wednesday

Dan Podsedly
Friday, September 26, 2008

Next week is the first Wednesday of October, and that means another New York City Ruby Happy Hour, sponsored by Outside.in and Pivotal.

Where: Outside.in, 20 Jay St Suite 1019 (10th Fl), Brooklyn, NY
When: 7-9PM, Wednesday September 3rd
Who: If you’re a developer who uses Ruby and would like to meet some other Ruby folks, toss around ideas, or just have a few beers, we welcome you with open arms!

There will be pizza, beer and wii-based entertainment for everyone. Click here for more details, and to RSVP.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter
Dan Podsedly

NYC Ruby Happy Hour at Outside.In

Dan Podsedly
Friday, August 29, 2008

Pivotal Labs is teaming up with Outside.In in New York City to co-host the monthly Ruby Happy Hour, on the first Wednesday of every month, starting Sep 3.

There will be pizza, beer and wii-based entertainment for everyone. Apparently these happy hours are getting quite popular, so please RSVP either in the comments here, or on Outside.In’s blog.

Where: Outside.in, 20 Jay St Suite 1019 (10th Fl), Brooklyn, NY
When: 7-9PM, Wednesday September 3rd
Who: If you’re a developer who uses Ruby and would like to meet some other Ruby folks, toss around ideas, or just have a few beers, we welcome you with open arms!

Outside.In

Outside.in (www.outside.in) is the web’s leading platform for neighborhood news and conversation. Outside.in’s technology dynamically maps web content to offline locations, which enables hyperlocal news discovery and sharing for consumers. The company also recently released GeoToolkit (www.outside.in/toolkit), which provides powerful tools for content creators of all sizes to optimize, promote and monetize their local content. Outside.in is supported by leading investors including Union Square Ventures, Milestone Venture Partners, Betaworks and the New York City Investment Fund. For more information, visit www.outside.in or the companyís blog at http://blog.outside.in.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

Topics

  • agile (778)
  • rails (113)
  • testing (86)
  • ruby (83)
  • ruby on rails (70)
  • jobs (62)
  • javascript (54)
  • techtalk (44)
  • rspec (38)
  • activerecord (29)
  • productivity (29)
  • gogaruco (29)
  • ironblogger (29)
  • git (28)
  • nyc (27)
  • rubymine (25)
  • mobile (22)
  • bloggerdome (20)
  • cucumber (20)
  • process (19)
  • pivotal tracker (19)
  • jasmine (19)
  • design (18)
  • ios (18)
  • webos (17)
  • objective-c (17)
  • android (16)
  • palm (16)
  • "soft" ware (16)
  • fun (15)
  • tracker ecosystem (15)
  • ci (15)
  • cedar (15)
  • rails3 (14)
  • performance (14)
  • bdd (14)
  • gem (13)
  • selenium (12)
  • css (12)
  • goruco (12)
  • bundler (12)
  • tdd (12)
  • meetup (11)
  • railsconf (11)
  • nyc-standup (11)
  • capybara (10)
  • mac (10)
  • mojo (10)
  • chef (10)
  • rubygems (9)
Subscribe to rails Feed
  1. ←
  2. 1
  3. ...
  4. 5
  5. 6
  6. 7
  7. 8
  8. 9
  9. 10
  10. 11
  11. 12
  12. →
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • 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 >