Skip to content

Issue with validates uniqueness and STI/polymorphic models on rails 6? #1245

@mjankowski

Description

@mjankowski

This feels similar to this long-ago bug - #203 - which was fixed here - #592

Model of:

class Classification < ApplicationRecord
  belongs_to :classifiable, polymorphic: true
  belongs_to :topic

  validates :classifiable_id, :uniqueness => { :scope=> [:topic_id, :classifiable_type] }
end

Spec of:

require "rails_helper"

describe Classification do
  it { should belong_to(:classifiable) }
  it { should belong_to(:topic) }

  it "ensures uniqueness" do
    create(:classification)
    should validate_uniqueness_of(:classifiable_id).scoped_to([:topic_id, :classifiable_type])
  end
end

Relevant output is:

Failures:

  1) Classification ensures uniqueness
     Failure/Error: should validate_uniqueness_of(:classifiable_id).scoped_to([:topic_id, :classifiable_type])
     
     ActiveRecord::SubclassNotFound:
       Invalid single-table inheritance type: Show is not a subclass of Shoulda::Matchers::ActiveRecord::Uniqueness::TestModels::Producu
     # ./spec/models/classification_spec.rb:9:in `block (2 levels) in <top (required)>'

That ::Producu on the very end there in the error makes me suspect that we're calling next on a string with Product (or something?) somewhere in the codebase.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions