Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mongoid related scopes in inherited models - fails in RC1
class Business
include Mongoid::Document
references_many :cleanliness_ratings
...
end
class Rating
include Mongoid::Document
referenced_in :business
scope :good, :where => {:score.gte => 7}
end
class CleanlinessRating < Rating
end
# In Mongoid 2.0.0.beta.20
business.cleanliness_ratings.good
#=> #<Mongoid::Criteria:0x000001051da6b8 @selector={"business_id"=>BSON::ObjectId4cd8277444939b0592003d70, :score=>{"$gte"=>7}}, options{}, klassCleanlinessRating, documents[]
# In Mongoid 2.0.0.rc.1
NoMethodError: undefined method `conditions' for nil:NilClass
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
from (eval):3:in `good'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/criteria.rb:181:in `block in method_missing'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/finders.rb:167:in `with_scope'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/criteria.rb:180:in `method_missing'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/relations/referenced/many.rb:308:in `block in method_missing'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/finders.rb:167:in `with_scope'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/mongoid-2.0.0.rc.7/lib/mongoid/relations/referenced/many.rb:307:in `method_missing'
from (irb):19
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/scott/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:9:in `require'
from script/rails:9:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment