Skip to content

Instantly share code, notes, and snippets.

@smoofles
Created June 6, 2011 08:32
Show Gist options
  • Save smoofles/1009941 to your computer and use it in GitHub Desktop.
Save smoofles/1009941 to your computer and use it in GitHub Desktop.
ruby-1.9.2-p0 :046 > Site.first.pages.where(:extradata => {"interview_number" => "MP1"}).count
NoMethodError: undefined method `new' for nil:NilClass
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers/strategies.rb:52:in `matcher'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers.rb:23:in `block (2 levels) in matches?'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers.rb:22:in `each'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers.rb:22:in `block in matches?'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers.rb:20:in `each_pair'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/matchers.rb:20:in `matches?'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/contexts/enumerable.rb:214:in `block in filter'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/contexts/enumerable.rb:214:in `select'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/contexts/enumerable.rb:214:in `filter'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/contexts/enumerable.rb:45:in `count'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/bundler/gems/mongoid-da404f47e1c2/lib/mongoid/criteria.rb:48:in `count'
from (irb):46
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /Users/smoofles/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from ./script/rails:6:in `require'
from ./script/rails:6:in `<main>'ruby-1.9.2-p0 :047 >
ruby-1.9.2-p0 :017 > Site.first.pages.where(:"extradata.age" => { :"$exists" => true }).first
NoMethodError: undefined method `new' for nil:NilClass
@paulelliott
Copy link

Try this:

Site.first.pages.where("extradata.interview_number" => "MP1").count

@smoofles
Copy link
Author

This works:

ruby-1.9.2-p0 :043 > @pages.where('published' => true).select{ |x| x.extradata && x.extradata["business_partners"] == "nein" }.count
=> 67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment