Skip to content

Instantly share code, notes, and snippets.

@mh-mobile
Last active April 19, 2020 14:38
Show Gist options
  • Save mh-mobile/11a3185e9d888fd1e96670613bf2db91 to your computer and use it in GitHub Desktop.
Save mh-mobile/11a3185e9d888fd1e96670613bf2db91 to your computer and use it in GitHub Desktop.
ActiveRecord::HasOneAssociationPolymorphicThroughError:
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "6.0.0"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :hardbacks, force: true do |t|
end
create_table :paperbacks, force: true do |t|
end
create_table :books, force: true do |t|
t.string :format_type
t.integer :format_id
end
create_table :dust_jackets, force: true do |t|
t.string :hardback_id
end
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Book < ApplicationRecord
belongs_to :format, polymorphic: true
has_one :dust_jacket, through: :format, source: :dust_jacket, source_type: "Hardback"
end
class Paperback < ApplicationRecord; end
class Hardback < ApplicationRecord
has_one :dust_jacket
end
class DustJacket < ApplicationRecord; end
class AuthorTest < Minitest::Test
def test_association
dust_jacket = DustJacket.new
hardback = Hardback.create!
hardback.dust_jacket = dust_jacket
book = Book.new
book.format = hardback
book.save!
book.reload
assert_equal dust_jacket, book.dust_jacket
end
end
Finished in 0.024135s, 41.4344 runs/s, 0.0000 assertions/s.
1) Error:
AuthorTest#test_association:
ActiveRecord::HasOneAssociationPolymorphicThroughError: Cannot have a has_one :through association 'Book#dust_jacket' which goes through the polymorphic association 'Book#format'.
/home/mbyk/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/reflection.rb:917:in `check_validity!'
/home/mbyk/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations/association.rb:43:in `initialize'
/home/mbyk/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations.rb:237:in `new'
/home/mbyk/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations.rb:237:in `association'
/home/mbyk/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations/builder/association.rb:100:in `dust_jacket'
has_one_polymorphic_association2.rb:67:in `test_association'
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment