Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Created November 23, 2010 18:03
Show Gist options
  • Save franckverrot/712202 to your computer and use it in GitHub Desktop.
Save franckverrot/712202 to your computer and use it in GitHub Desktop.
default_scope without proc on Rails 2.3.5
# In the model
class Foo < ActiveRecord::Base
default_scope Hash.new {|hash, key| key == :conditions ? {:baz => I18n.locale.to_s } : nil }
named_scope :bar, lambda { { :conditions => { :bar => 1..10 } } }
end
# In action
ree-1.8.7-2010.02 > I18n.locale = 'fr'
=> "fr"
ree-1.8.7-2010.02 > Foo.bar
Foo Load (0.2ms) SELECT * FROM "foos" WHERE (("foos"."baz" = 'fr' AND "foos"."bar" BETWEEN 1 AND 10))
=> []
ree-1.8.7-2010.02 > I18n.locale = 'en'
=> "en"
ree-1.8.7-2010.02 > Foo.bar
Foo Load (0.3ms) SELECT * FROM "foos" WHERE (("foos"."baz" = 'en' AND "foos"."bar" BETWEEN 1 AND 10))
=> []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment