Dr Nic

Find objects in IRB directly from browser URLs

A long time ago, I tired of going into the irb/console and finding objects/models using the traditional ActiveRecord command Person.find(15) and now I’m sitting pretty: I can paste in URLs to fetch objects.

# No more of this:
=> Person.find(15)
# instead:
=> people/15

people/15 is something you’ll copy+paste directly from your browser: http://localhost:3000/people/15

Of course, the url is based on your routing + controllers, so the assumption here is that your routes/controllers map to your active record models. That is, your app is smothered in RESTful love and cuddles.

Not following this? Here’s a video:

How to make this work at home

Copy and paste the following into your .irbrc file:

Thanks goes to…

The some original code for this comes via Mike Clark, who had the idea for syntax activity(6). This was good.

I previously had another idea to support the syntax 6.to_activity using the RubyGem to_activerecord. I still like the id.to_class_name structure of this and still use it.

But if I have a perfectly nice looking url sitting in front of me, I can now paste the class_name/id part into irb and I’m off and running.

Happy New Year.

Related posts:

  1. MagicCGI shows OpenID user count In the last 20 days, 43 people have used...
  2. Magic Multi-Connections: A “facility in Rails to talk to more than one database at a time” At this point in time there’s no facility in Rails...
  3. [BTS] Magic Models vs ActiveRecords – Efficiency Dr Nic’s Magic Models are magical, that goes without saying....
  4. [BTS] Magic Models – Class creation [BTS] = Behind the Scenes; also a news-like TV show...

12 Responses to “Find objects in IRB directly from browser URLs”

  1. mislav says:

    I like it a lot.

    So how about nested routes? … Just kidding :) or am I?

  2. Dr Nic says:

    Ooh, I forgot the hook! You’ll need to add the following to the bottom of your .irbrc file:

    IRB.conf[:IRB_RC] = Proc.new { define_model_find_shortcuts }
  3. Dr Nic says:

    @mislav [via] – ok, figured out how to support has_many nested_routes, e.g. people/123/addresses

    See the pastie

    NOTE: This assumes there are models Person and Address, and Person.has_many :addresses


  4. @drnic Hey Gem master, what about packaging all of that in a nice gem a la wirble?

  5. saimon says:

    You’ve now got no excuse to add in support for all restless urls (as far as logical) and package it up into a gem. DO IT NOW :)

  6. saimon says:


    This handles url of the type: /users/david

    Now only need to figure out /users/david-myopenid-com and we’re all set.


  7. Dr Nic says:

    @saimon [via] – Very nice work :)

    I added the following when clause to the #evaluate method to support development urls (e.g. http://localhost:3000...)

            when /^(http|https):\/\/[a-z.]+:[0-9]+(([0-9]{1,5})?\/.*)?$/ix
              original_evaluate($2.gsub(/(^\/)/,''), line_no)
  8. saimon says:

    @Dr Nic [via] – Cool stuff…Looking good…

  9. Matthijs Langenberg says:

    While this is a neat little trick, I really don’t like the tight coupling between the way to access the resource at HTTP level and database level.

  10. Dr Nic says:

    Matt Aimonetti noted that outside of script/console, there was an error with pluralize missing from String class. #pluralize is an active_scaffold contribution.

    I realise I have the following at the top of my .irbrc file:

    unless (IRB.conf[:LOAD_MODULES] || []).join =~ /config\/environment/
      require 'active_support'

    That is, if you’re in irb, and /config/environemtn isn’t loaded, then explicitly load active_support.