skip to Main Content

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"]

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search