Created
May 19, 2011 12:27
-
-
Save jewilmeer/980626 to your computer and use it in GitHub Desktop.
lambda caching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Episode < ActiveRecord::Base | |
scope :airs_at_in_future, lambda{ where('episodes.airs_at > ?', Time.zone.now) } | |
scope :airs_at_in_past, lambda{ where('episodes.airs_at < ?', Time.zone.now) } | |
# will cache Time.now | |
scope :next_airing, airs_at_in_future.order('episodes.airs_at asc') | |
scope :last_aired, airs_at_in_past.order('episodes.airs_at desc') | |
# will not cache Time.now | |
scope :next_airing, lambda{ airs_at_in_future.order('episodes.airs_at asc') } | |
scope :last_aired, lambda{ airs_at_in_past.order('episodes.airs_at desc') } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess Arel/AR only postpones (lazyloads) the sql call, and call's procs directly
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/named_scope.rb#L160