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 4/26/2010: Ruby hack night with Sarah Mei

David Stevenson
Monday, April 26, 2010

Interesting Things

  • Given a list of IDs, how do you find which ones are not in the database?
ids = [1,2,3,4,5,0]
missing_ids = ids - Model.find_all_by_id(ids).collect(&:id)
  • Sarah Mei is hosting a Ruby Hack Night at pivotal labs tomorrow at 7PM (Tues April 27th. 731 Market St Floor 3, San Francisco).
  • There is an android users group tomorrow. We don’t know any more about it than that.
  • 0 Shares
  • Share on Facebook
  • Share on Twitter

5 Comments

  1. WT says:

    You don’t want to instantiate those objects. Wow.

    Use #select or use a method from ActiveRecord::ConnectionAdapters::DatabaseStatements.

    ids = [1,2,3,4444444]
    missing_ids = ids – Model.select(:id).where(id: ids).all.map(&:id)

    April 26, 2010 at 11:49 pm

  2. Sam says:

    Yep, or pre r3:

    ids – Model.all(:conditions => {:id => ids}, :select => [:id]).collect(&:id)

    I thought that exists? might work for this, but you’d have to iterative over ids, generating loads more queries than you need. You can pass it conditions, but it will return a simple ‘true’ if ANY of the records have that ID.

    April 27, 2010 at 4:05 pm

  3. David Stevenson says:

    Regardless of WT or Sam’s solution, you’re still instantiating objects. Thanks for pointing out that we should only be brining back the IDs, however, that can be a big performance win. If you really only want the IDs and not objects, you’d have to write straight SQL (which isn’t very rails friendly):

    ids – Model.connection.select_values(“SELECT `#{Model.primary_key}` FROM `#{Model.table_name}` WHERE `#{Model.primary_key}` IN (#{ids.join(“,”)})”).collect(&:to_i)

    The above statement doesn’t use adapter-specific column or table name quoting, which would have made it even more generic for non-mysql adapters.

    April 28, 2010 at 10:33 am

  4. Joseph Palermo says:

    Also, it wasn’t stated originally, but there was an object caching layer (via [cache money](http://github.com/nkallen/cache-money) I think) that direct SQL access would have bypassed.

    April 28, 2010 at 11:54 pm

  5. fetion says:

    You can pass it conditions, but it will return a simple ‘true’ if ANY of the records have that ID.

    remove

    May 4, 2010 at 7:29 pm

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 >