Single table inheritance strangeness

Posted by jacqui maher on April 29, 2008 at 10:19 PM

I suppose “special” is a word for this behavior. I would have expected a constraint used in find_by_id to also be used in find. Right.


>> Asset.find_by_id(params[:id])
# => nil

log: Asset Load (0.009816) SELECT * FROM assets WHERE (assets.`id` = ‘8515’) AND ( (assets.`class_name` = ‘Asset’ ) ) LIMIT 1

>> Asset.find(params[:id])
# => #“IdamAsset”, “updated_date”=>”2008-04-14 20:49:58”, “restrictions”=>nil, “idam_iud”=>”19013896”, “name”=>”6da9.jpg”, “hide”=>”0”, “is_pending”=>”0”, “is_administrative”=>”0”, “created_date”=>”2008-04-14 20:49:58”, “security_level”=>”1”, “media_type_id”=>”10”, “id”=>”8515”, “description”=>”“, “filename”=>”6da9.jpg”, “last_synced_at”=>nil, “uploaded_by_id”=>”448”}>

log: BaseAsset Load (0.000528) SELECT * FROM assets WHERE (assets.`id` = 8515)

Comments: 0 (view/add your own) Tags: rails, sti

print out a controller's expected actions

Posted by jacqui maher on April 26, 2008 at 04:59 PM

Ever want to see a list of actions defined on your controller and what methods over http they expect?

try this in your console, where “users” is your controller’s name:

   1  ActionController::Routing::Routes.routes.select do |route|
2 route.defaults[:controller] == "users"
3 end.uniq.each do |route|
4 puts "#{route.defaults[:action]} expects http method: #{route.conditions[:method]}"
5 end;nil

Comments: 0 (view/add your own) Tags: meta, ruby

model_stubbing in Rails 1.2.x

Posted by jacqui maher on April 18, 2008 at 03:58 PM

I really like technoweenie’s plugin model_stubbing. It’s a nice alternative to fixtures, and an even nicer way to create stubs in your tests and specs.

Unfortunately the StreetEasy codebase is still on Rails 1.2.x which caused compatibility problems with the plugin. Rails 2 refactored Fixtures and database statements a bit.

I had to modify the plugin to get it to work with older versions of Rails. I forked the project on github and post about it to help anyone who’d like to use the plugin but is not upgraded to Rails 2 yet.

github homepage: forked model_stubbing
public clone url: git@github.com:jacqui/model_stubbing.git

[edit] btw, simplelog was munging the href for the public clone url; it seems to like prepending http:// to links, so i just removed the anchor tags from it.

make localhost

Posted by jacqui maher on April 15, 2008 at 12:58 PM

Hey Rails developers: I bet you do the majority of your development locally, but work in an environment where you have various other development, staging, testing, and production servers.

I often have to check the same URL locally and on our staging or production servers. I got tired of selecting the hostname and changing it to “localhost:3000” and vice versa, so I made myself a bookmarklet. It is *really* easy to make a bookmarklet.

Here’s a bookmarklet that will replace everything up to the gTLD (I took this list from wikipedia) with “localhost:3000”. Drag it to your bookmarks toolbar, and the next time you’re on a url in your application that you’d like to view locally, just click it.

make localhost

here’s the source. enjoy…

   1  <a href="javascript:var current_url = location.href; var new_url = current_url.replace(/.*?(\.com|\.net|\.org|\.mil|\.info|\.gov|\.biz|\.jobs|\.int|\.pro|\.ae    ro|\.asia|\.cat|\.coop|\.edu|\.mobi|\.museum|\.name|\.travel|\.tel)\//, 'http://localhost:3000/'); location.href = new_url; ">make localhost</a>


I viewed that in my browser and dragged the link to my bookmarks toolbar. Now I can be on any of our servers ending in “.streeteasy.com” and in a single click view the page locally.

I found myself having to reverse logic in some code I wrote recently due to the wants of the client doing a 180. I had been using reject! to remove elements in an array, and realized that I now wanted to use select! - only there is no select!

I started to write:

arr = arr.select{|a| some.method.here }

when it occured to me that was a little clunky. I wanted select in place.

Here’s my version of it:

   1  class Array
2 def select!(&block)
3 selected = self.select(&block)
4 if selected.size > 0
5 replace(selected)
6 else
7 nil
8 end
9 end
10 end


Usage:

   1  >> arr = ["Ren", "Easy-E", "Ice Cube", "DJ Yella", "Dr Dre"]
2 # => ["Ren", "Easy-E", "Ice Cube", "DJ Yella", "Dr Dre"]
3
4 >> arr.select!{|a| a =~ /^D/ }
5 # => ["DJ Yella", "Dr Dre"]
6
7 >> arr.select!{|a| a =~ /^J/ }
8 # => nil
9
10 >> arr
11 # => ["DJ Yella", "Dr Dre"]