Uninitialized constant User::View

(Ryan Fox) #1

NameError (uninitialized constant User::View)

This error came up when trying to call as_json on User models for my webhooks plugin. I don’t really know much about Rails to give any insight into why this is happening.

I kinda just hacked around the issue by creating a UserSerializer and returning a serializable_hash from that. I’m guessing there’s a better way to do it though…

Here’s the backtrace from where I call into it:

/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/inheritance.rb:133:in `compute_type'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/reflection.rb:221:in `klass'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/associations/association.rb:118:in `klass'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/associations/collection_association.rb:36:in `reader'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/associations/builder/association.rb:110:in `views'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activemodel-4.1.8/lib/active_model/serialization.rb:108:in `block in serializable_hash'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activemodel-4.1.8/lib/active_model/serialization.rb:108:in `each'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activemodel-4.1.8/lib/active_model/serialization.rb:108:in `serializable_hash'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.8/lib/active_record/serialization.rb:17:in `serializable_hash'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activemodel-4.1.8/lib/active_model/serializers/json.rb:99:in `as_json'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/core_ext/object/json.rb:159:in `block in as_json'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/core_ext/object/json.rb:159:in `each'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/core_ext/object/json.rb:159:in `map'
/var/www/discourse/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.8/lib/active_support/core_ext/object/json.rb:159:in `as_json'
/var/www/discourse/plugins/Discourse-Webhooks/plugin.rb:30:in `block (3 levels) in activate!'

(Sam Saffron) #2

hmm … perhaps we should be loading that module earlier during initialization. sounds like a missing dependency somewhere.

(Ryan Fox) #3

Not sure if I’m understand, but this is happening in a DiscourseEvent callback, so it should be well after initialization. (The event itself is registered in an after_initialize block.)

(Kane York) #4

I don’t think that the code actually defines User::View anywhere. What code is trying to use it…?

(Ryan Fox) #5

It’s code in a plugin that I wrote. I’m able to call as_json on a Post model, but not on a User model.

(Kane York) #6

Why not use the PostSerializer / UserSerializer?

(Ryan Fox) #7

I’m handling DiscourseEvent triggers for my webhooks plugin. At the time of handling the events, I have no idea what parameters are being passed in, and I just want turn them into JSON.

I ended up monkey patching User.as_json to use the UserSerializer, but that doesn’t seem like the right way of doing it for general use.

Maybe I’m wrong, but this feels like an oversight in the code. It seems like as_json should work for all models.

(Kane York) #8

Just do this…

 MultiJson.dump(UserSerializer.new(user, scope: Guardian.new(current_user)))

(Ryan Fox) #9

My point is that I don’t know when I have a user or a post or some random hash. I need to be able to treat them all the same.

(Kane York) #10

Use a type-switch then?

(Ryan Fox) #11

I hadn’t considered that. (I’ve never touched Ruby before needing to hack on Discourse.) Thanks.

(Régis Hanol) #12

Just had the exact same issue when fixing

Smallest reproduction steps: execute User.new.as_json in the rails console.

Not sure what we did to the User model to make it break that way :frowning:

(James Kiesel) #13


(Régis Hanol) #14

That was it. Thanks :thumbsup:

(Régis Hanol) #15