简体   繁体   中英

uninitialized constant hit on nested model when running test suite

The following class

class Producttag < ApplicationRecord
  belongs_to :product, optional: true
  belongs_to :variant, optional: true

is a child of its parent

class Product < ApplicationRecord
  has_many   :productags, dependent: :destroy

whom itself is in a chain of two other parents. Each class calls dependent: :destroy on its children.

When running tests on a destroy action, each class encounters the same error:

NameError: uninitialized constant Product::Productag

or on the variant class

NameError: uninitialized constant Variant::Productag

all other parnet child relations dependencies get destroyed, but not this latter one. The only difference that is apparent is the optional: true placed on the relationship. This is an assumption regarding the error in the destroy action, but hard to comprehend.

If this is the motive, why? or what is missing then to allow destruction of the 'optional' child records?

The class is Producttag but your relation is has_many:productags, dependent: :destroy .

You are missing a "t" in product t ag and this is causing rails to look for a "class_name" Productag

That being said ProductTag and product_tag would have made this a lot more obvious

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM