Jon Allured

Computer Programmer, Whiskey Drinker, Comic Book Reader

Multiple Carets in TextMate 2.0; Default Scope

published 12/19/11

Don't get me wrong, I'm quickly learning to love VIM since that's all we use at Hashrocket, but TextMate will always be the first text editor that I loved. I haven't been following the Alpha that closely, but I did read about the new Multiple Caret features and I'm really excited about it, I think it looks pretty great.


Found out about default_scope on Destroy All Software 21: Coupling and Abstraction. When you do:

User.all

And you've got:

class User < ActiveRecord::Base
  default_scope where(active: true)
end

Then the all method will use that default and only return active users. Pretty neat.

Gregory warns though, that you can easily become coupled to this behavior in a bad way and advises that if you actually want to use this default, you might want to try using an alias instead:

class User < ActiveRecord::Base
  default_scope where(active: true)

  class << self
    alias_method :all_active_users, :all
  end
end

And then you can use User.all_active_users instead. What's nice about this approach is that if you later want to change the default scope, you'll notice that the all_active_users is using it and can make a better choice about your change.