Last active
April 19, 2020 03:18
-
-
Save JunichiIto/0b4293d7210666003b3bcde27cc651a4 to your computer and use it in GitHub Desktop.
Try to run the conde in https://guides.rubyonrails.org/association_basics.html#options-for-has-many-source-type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
8: from ./sample.rb:49:in `<main>' | |
7: from ./sample.rb:50:in `<class:Book>' | |
6: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations.rb:1511:in `has_one' | |
5: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations/builder/association.rb:30:in `build' | |
4: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations/builder/association.rb:40:in `create_reflection' | |
3: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-6.0.0/lib/active_record/associations/builder/association.rb:67:in `validate_options' | |
2: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.0/lib/active_support/core_ext/hash/keys.rb:50:in `assert_valid_keys' | |
1: from /Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.0/lib/active_support/core_ext/hash/keys.rb:50:in `each_key' | |
/Users/jnito/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/activesupport-6.0.0/lib/active_support/core_ext/hash/keys.rb:52:in `block in assert_valid_keys': Unknown key: :polymorphic. Valid keys are: :class_name, :anonymous_class, :foreign_key, :validate, :autosave, :foreign_type, :dependent, :primary_key, :inverse_of, :required, :as, :touch (ArgumentError) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.integer :author_id | |
t.string :format_type | |
t.integer :format_id | |
end | |
create_table :authors, force: true do |t| | |
end | |
end | |
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
end | |
class Author < ApplicationRecord | |
has_many :books | |
has_many :paperbacks, through: :books, source: :format, source_type: "Paperback" | |
end | |
class Book < ApplicationRecord | |
has_one :format, polymorphic: true | |
# Should change to belongs_to? | |
# belongs_to :format, polymorphic: true | |
end | |
class Hardback < ApplicationRecord; end | |
class Paperback < ApplicationRecord; end | |
class AuthorTest < Minitest::Test | |
def test_association | |
author = Author.create! | |
paperback = Paperback.create! | |
book = Book.new | |
book.format = paperback | |
book.author_id = author.id | |
book.save! | |
author.reload | |
assert_equal paperback, author.paperbacks.first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment