Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `[]' for nil:NilClass when using as_json #87

Closed
jwoertink opened this issue Jan 20, 2013 · 3 comments
Closed

undefined method `[]' for nil:NilClass when using as_json #87

jwoertink opened this issue Jan 20, 2013 · 3 comments

Comments

@jwoertink
Copy link

I have a simple Photo model

class Photo
  include Mongoid::Document
  mount_uploader :photo_image, PhotoUploader

  belongs_to :attachable, polymorphic: true
end

My photos upload and save fine, but when I call as_json I get an undefined method [] for nil class.

1.9.3p362 :009 > Photo.first.as_json
NoMethodError: undefined method `[]' for nil:NilClass
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/carrierwave-mongoid-0.3.0/lib/carrierwave/mongoid.rb:67:in `block in serializable_hash'
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/carrierwave-mongoid-0.3.0/lib/carrierwave/mongoid.rb:66:in `each'
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/carrierwave-mongoid-0.3.0/lib/carrierwave/mongoid.rb:66:in `serializable_hash'
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/activemodel-3.2.11/lib/active_model/serializers/json.rb:96:in `as_json'
    from (irb):9
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /Users/jeremy/.rvm/gems/ruby-1.9.3-p362@tabeso/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Now, if I pass an empty hash, it works.

1.9.3p362 :010 > Photo.first.as_json({})
 => {"_id"=>"50f5d85fd86367cc3c000001", "attachable_field"=>nil, "attachable_id"=>"50f49d74d863677939000134", "attachable_type"=>"Event", "photo_image"=>{"url"=>"", :thumb=>{"url"=>""}}, "primary"=>true, "url"=>nil}

I think just changing the default argument in this method from nil to {} should fix it.
https://github.com/jnicklas/carrierwave-mongoid/blob/master/lib/carrierwave/mongoid.rb#L64

@rmm5t
Copy link
Member

rmm5t commented Jan 21, 2013

This should have been fixed in #81. Instead of v0.3.0, please try carrierwave-mongoid 0.3.1 or 0.4.0.

Let me know if that does the trick.

@rmm5t
Copy link
Member

rmm5t commented Jan 31, 2013

@jwoertink Was this resolved for you in carrierwave-mongoid 0.3.1?

@jwoertink
Copy link
Author

@rmm5t yeah, that works! thanks! Looks like I'm good now, so I'll close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants