Red Squirrel Stumblog RSS

Dave Hoover stumbles through technology

Apr
19th
Sat
permalink

History Blog Meme

history 1000 | awk ‘{a[$2]++}END{for(i in a){print a[i] ” ” i}}’ | sort -rn | head
95 cd
72 ls
52 svn
49 rake
27 exit
24 p
22 sudo
20 mimi
16 cap
15 git
via objo
Mar
19th
Wed
permalink

One man's fight to disable Ferret during tests/specs

module ActsAsFerret
  module InstanceMethods
    # Overriding these so we don't hit DRb
    def ferret_create; true; end
    def ferret_update; true; end
    def ferret_destroy; true; end
  end
end
Mar
6th
Thu
permalink

Reverting in git

git reset —hard
Feb
22nd
Fri
permalink

TextMate regex for removing newlines in CSV

Find: (,"[^"]+)\n([^"]+")
Replace: $1 $2
This will need to be run repeatedly until it doesn’t match anymore because it only removes one newline from a field at a time and some fields may have multiple newlines.
Feb
21st
Thu
permalink

Combined logging for Sinatra and ActiveRecord

ActiveRecord::Base.logger = Logger.new(SOME_LOG_NAME)
Sinatra::Environment.prepare_loggers(ActiveRecord::Base.logger)
Feb
1st
Fri
permalink

Close all tabs in TextMate

Command - Control - W
Dec
31st
Mon
permalink

dup vs. clone

You might think that ActiveRecord::Base#dup is what you want, but you really want clone.
Dec
9th
Sun
permalink

ActiveRecord's QueryCache#uncache

Sometimes you need to compare an attribute of an in-memory ActiveRecord instance against its attribute in the database. The way I usually do that is to grab a fresh instance from the database like this:

class Something < ActiveRecord::Base
  def previous(attribute)
    self.class.find(id)[attribute]
  end
end

That worked just fine in Rails 1.2. But Rails 2.0 has request-scoped query caching enabled by default, meaning that if you have already executed the same SQL that is generated by find(id), you’re going to get the exact same instance back from that call to find. This makes it impossible to compare in-memory attributes against attributes stored in the database.

The solution is to use ActiveRecord’s uncache method. This is a class method, so you’ll need to grab your class to call it like this:

class Something < ActiveRecord::Base
  def previous(attribute)
    self.class.uncached { self.class.find(id)[attribute] }
  end
end
Dec
3rd
Mon
permalink

finder_sql String gets eval'd later

If you ever need to use ActiveRecord’s has_many with finder_sql, remember that the key to finder_sql is that the String get eval’d in the context of the ActiveRecord instance rather than the ActiveRecord class. So make sure you use single quotes! This prevents the String from being interpolated too soon.

Do this:
has_many :foos,
  :finder_sql => 'select ... where some_id=#{id}'
Not this:
has_many :foos,
  :finder_sql => "select ... where some_id=#{id}"
In the former case, the #{id} will be the id of the ActiveRecord instance. In the latter case, the #{id} will be the id of the ActiveRecord class.
Nov
16th
Fri
permalink

Watch out for ISO-8859-1 on Rails

Rails uses a UTF-8 charset by default. If you have latin1 on the backend, you’ll end up with a questions mark (yes, a “?”) instead of a ® on the front end. Here’s one way to fix it, but be sure to heed Brian’s comment.