Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## lib/ext/exclusive_scope.rb
- module ActiveRecord
- module NamedScope
- class ExclusiveScope < Scope
- def initialize parent_scope, options = {}
- super(parent_scope, options)
- end
- private
- def method_missing *args, &block
- with_scope({}, :overwrite) { super }
- end
- end
- end
- end
- class << ActiveRecord::Base
- def exclusive_scope options = {}
- ActiveRecord::NamedScope::ExclusiveScope.new self, options
- end
- end
- ## app/models/event.rb
- class Event < ActiveRecord::Base
- default_scope :conditions => { :campaign_id => nil }
- # ...
- def self.campaign campaign
- ActiveRecord::NamedScope::ExclusiveScope.new(self, :conditions => { :campaign_id => campaign })
- end
- # ...
- end
- ## console
- >> Event.all
- => []
- >> Event.campaign Campaign.first
- => [#<Event id: 1, name: "foo", created_at: "2009-11-25 22:34:26", updated_at: "2009-11-25 22:34:26", user_id: 1, start_time: "2009-11-25 22:34:26", end_time: "2009-11-26 00:34:26", description: nil, metro_area_id: nil, location: nil, street_address: nil, address_line_2: nil, city: nil, us_state: nil, zip_code: nil, delta: true, reminded_at: nil, campaign_id: 1>]
- >>
Add Comment
Please, Sign In to add comment