Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: wrong constant name

I have a has_many / belongs_to polymorphic relationship between two models, PointModifier and ManualPoints.

# ManualPoints Model
has_many :point_modifiers, as: :modifier

# PointModifier Model
belongs_to :modifier, polymorphic: true

I recently started seeing this error in my tests - NameError: wrong constant name - after adding ActiveModel::Serializer into my project. It does not appear to be related to AMS, and I've never seen this error before.

like image 567
Aaron Gray Avatar asked Feb 15 '26 20:02

Aaron Gray


1 Answers

A little sleuthing revealed that this error appears to be related to constants.

http://ruby-doc.org/core-1.9.3/NameError.html

Some more digging and I realized that the issue was related to my fixtures.

# PointModifiers Fixture
one:
  points: 2
  user_id: 1
  modifier_type: manual_points
  modifier_id: 1

The issue was that I had incorrectly used snake_case for the polymorphic relationship in the fixture. The solution is to use CamelCase, like this:

# PointModifiers Fixture
one:
  points: 2
  user_id: 1
  modifier_type: ManualPoints
  modifier_id: 1
like image 127
Aaron Gray Avatar answered Feb 18 '26 11:02

Aaron Gray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!