Rails 7.1, “Is Invalid” on has_many through: Associated Polymorphic Class

When trying to save a new Product::Group, with many Product::Features, I get the error “Features is invalid”

This below gives the error.

class Product::ObjectFeature < ApplicationRecord
    self.table_name = "object_features"
    belongs_to :feature, class_name: "Product::Feature", foreign_key: :feature_id, inverse_of: :object_features
    belongs_to :featurable, polymorphic: true
end

class Product::Feature < ApplicationRecord
    self.table_name = "features"
    has_many :object_features, foreign_key: :feature_id, inverse_of: :feature
end

class Product::Group < ApplicationRecord
    self.table_name = "product_groups"
    has_many :object_features, as: :featurable, inverse_of: :featurable
    has_many :features, through: :object_features, class_name: "Product::Feature", foreign_key: :feature_id
end

However, when I drop the Product and move those files to the root, and clean up the associations it works.

class ObjectFeature < ApplicationRecord
    belongs_to :feature
    belongs_to :featurable, polymorphic: true
end

class Feature < ApplicationRecord
    has_many :object_features
end

class Product::Group < ApplicationRecord
    self.table_name = "product_groups"
    has_many :object_features, as: :featurable
    has_many :features, through: :object_features
end

What am I doing wrong? I have run through every possible combination of inverse_of, class_name, primary_key, foreign_key, source, source_type, etc. between these 3 classes.

I finally gave up and just left them in the root of models, however this is just a temporary solution until I can figure out why its not working.

Anyone have a solution?

———————–ERROR———————-

Processing by Catalog::GroupsController#create as TURBO_STREAM

Parameters: {"authenticity_token"=>"[FILTERED]", "product_group"=>{"arrangement_id"=>"1", "function_pair_id"=>"117", "product_cover_ids"=>[""], "collection_name"=>"", "loading"=>"", "spec_sheet_template"=>"upholstery_group", "feature_ids"=>["", "54", "19", "40"]}, "commit"=>"Create Group", "product_id"=>"974"}

User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]

Product::Feature Load (0.2ms)  SELECT "features".* FROM "features" WHERE "features"."id" IN ($1, $2, $3)  [["id", 54], ["id", 19], ["id", 40]]

app/controllers/default_controller.rb:111:in `set_create_object'

Catalog::Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 974], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:157:in `set_product'

Role Load (0.1ms)  SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1  [["user_id", 1]]

app/models/ability.rb:9:in `map'

CACHE Product::Feature Load (0.0ms)  SELECT "features".* FROM "features" WHERE "features"."id" IN ($1, $2, $3)  [["id", 54], ["id", 19], ["id", 40]]

app/controllers/catalog/groups_controller.rb:47:in `create'

TRANSACTION (0.2ms)  BEGIN

app/controllers/catalog/groups_controller.rb:49:in `create'

Catalog::Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 974], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

Product::Function Load (0.1ms)  SELECT "function_pairs".* FROM "function_pairs" WHERE "function_pairs"."id" = $1 LIMIT $2  [["id", 117], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

Product::Arrangement Load (0.1ms)  SELECT "arrangements".* FROM "arrangements" WHERE "arrangements"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

ActiveStorage::Attachment Load (0.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 54], ["record_type", "Product::Feature"], ["name", "image"], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

ActiveStorage::Attachment Load (0.1ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 19], ["record_type", "Product::Feature"], ["name", "image"], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

ActiveStorage::Attachment Load (0.2ms)  SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4  [["record_id", 40], ["record_type", "Product::Feature"], ["name", "image"], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

Product::Group Exists? (0.2ms)  SELECT 1 AS one FROM "product_groups" WHERE "product_groups"."arrangement_id" = $1 AND "product_groups"."function_pair_id" = $2 AND "product_groups"."product_id" = $3 LIMIT $4  [["arrangement_id", 1], ["function_pair_id", 117], ["product_id", 974], ["LIMIT", 1]]

app/controllers/catalog/groups_controller.rb:49:in `create'

TRANSACTION (0.2ms)  ROLLBACK

app/controllers/catalog/groups_controller.rb:49:in `create'

["Features is invalid"]

3

Define the classes properly:

class Product < ApplicationRecord
  class ObjectFeature < ::ApplicationRecord
    self.table_name = "object_features"
    belongs_to :feature
    belongs_to :featurable, polymorphic: true
  end
end

class Product < ApplicationRecord
  class Feature < ::ApplicationRecord
    self.table_name = "features"
    has_many :object_features
  end
end

class Product < ApplicationRecord
  class Group < ::ApplicationRecord
    self.table_name = "product_groups"
    has_many :object_features, 
       as: :featurable, inverse_of: :featurable
    has_many :features, through: :object_features
  end
end

class Product::ObjectFeature is despite the popular misconception not actually a shorthand syntax. It’s misusing an operator that wasn’t designed for the purpose to get wonky results. Unfortunately the Rails generators (and guides) are pretty lazily written and just reinforce this bad practice.

The difference is that reopening the class will set the module nesting to [Product, Product::ObjectFeature] so that you can reference other classes inside the same class while it will be just [Product::ObjectFeature] if you use the scope resolution operator.

This means it can only find constants in the outer scope or in the class itself – because thats where you defined the class.

class Foo
  TEST = "I'm defined in Foo"
end
# bad
class Foo::Bar
  puts TEST # uninitialized constant Foo::Bar::TEST (NameError)
end
# good
class Foo
  class Bar
    puts TEST # I'm defined in Foo
  end
end

See the Ruby style guide.

5

Although I don’t know why, the issue has been solved.

by adding validate: false to the has_many :through, it allowed the items to be saved.

class Product::Group < ApplicationRecord
    self.table_name = "product_groups"
    has_many :object_features, as: :featurable
    has_many :features, through: :object_features, validate: false
end

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật