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

Standup 09/23/2008: Disabling pre-rails-2.1 style :include

David Stevenson
Tuesday, September 23, 2008

Interesting Things

  • If your HTTP header’s HTTP_CLIENT_IP is not equal to HTTP_X_FORWARDED_IP, then rails 2.1 and above will consider it an IP spoofing attack and throw an exception! This is bad news for some traditional Apache->Mongrel setups. Solution is probably to change the apache HTTP headers, but we’re wondering exactly why this is a security problem for rails (and why they would break compatibility with the default apache setup from way back when)?
  • Be careful when using validates_uniquess_of with :case_sensitive => true AND a unique index at the database level. If your database is case insenitive, then rails will approve the uniqueness, but the database will fail the insert. Solution: be sure to use a collation type for the unique column that is case sensitive (such as binary in mysql).
  • Rails 2.1+ :includes are way better than pre-2.1, but they are less compatible with conditions. Hence, rails falls back on the old style. Here’s when it might legitimately fall back:
User.find(:all, :include => :profile, :conditions => "profiles.gender = 'M'")

Because we reference the included table profiles in the :conditions, rails has no choice but to construct one giant query to fetch Users and their profiles, rather than a separate query. Here’s a case when it guesses wrong:

User.find(:all, :include => :profile,
  :joins => "INNER JOIN comments ON comments.user_id = users.id",
  :conditions => "comments.approved = 1")

Because the conditions references a table that is not users, rails thinks it has to fall back to the old include style… but it’s wrong! Here’s how we tricked ActiveRecord into always using rails 2.1+ includes (note that we had to fix up a few queries that were referencing :inlcuded tables in :conditions to make this work):

module ActiveRecord::Associations::ClassMethods
  private
  def references_eager_loaded_tables?(options)
    false
  end
end
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

4 Comments

  1. Tim Connor says:

    Vis a vis, the spoofing exceptions, we had the same problem at work. “I monkey-patched rails to turn off the ip spoofing check”:http://github.com/timocratic/rails/commit/f403857fc2ff7918b17dc315b45a1d55016d0fbb but I’d gladly submit a patch to make it a configuration setting to turn it on or off, if there is interest.

    September 24, 2008 at 3:07 am

  2. John says:

    Could you explain (or provide a link to) the differences between pre-2.1 and 2.1+ :include behavior? It makes the difference between a really helpful post and a nudge below the threshold for keeping a feed in my reader.

    September 24, 2008 at 11:38 pm

  3. David Stevenson says:

    Examples of pre-2.1 and 2.1 includes I posted in the [next standup article](http://pivots.pivotallabs.com/users/stevend/blog/articles/515-standup-09-24-2008-why-does-my-jvm-crash-running-solr-)!

    September 25, 2008 at 2:07 am

  4. Dan Manges says:

    The IP spoofing exception was fixed in Rails 2.1.1. There was some concern in the lighthouse ticket that the fix wasn’t sufficient, but I tested with Apache and Passenger and it fixed 100% of the occurrences for my site.

    October 2, 2008 at 3:07 am

Add New Comment Cancel reply

Your email address will not be published.

David Stevenson

David Stevenson
San Francisco

Recent Posts

  • 03/13/13: [SF] Use postgres!
  • Standup for 2/9/2012: NP in P time? As usual, no.
  • Standup 2/6/2012: Tracker UI updates
Subscribe to David's Feed

Author Topics

agile (10)
devise (1)
ec2 (1)
engines (1)
ios (1)
activerecord (3)
inverse_of (1)
css (2)
engineyard (1)
iphone (3)
rubymine (1)
rails (5)
aws (1)
javascript (1)
xmpp (1)
gogaruco (3)
activeresource (1)
heroku (1)
solr (1)
bug (1)
patch (1)
  • 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 >