Skip to content

Instantly share code, notes, and snippets.

@claudiob
Created February 26, 2016 17:08
Show Gist options
  • Save claudiob/e453503c47074bab1617 to your computer and use it in GitHub Desktop.
Save claudiob/e453503c47074bab1617 to your computer and use it in GitHub Desktop.

Revisions

  1. claudiob created this gist Feb 26, 2016.
    64 changes: 64 additions & 0 deletions test_polymorphic_validates_associated.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    begin
    require 'bundler/inline'
    rescue LoadError => e
    $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
    raise e
    end

    gemfile(true) do
    source 'https://rubygems.org'
    gem 'rails', '5.0.0.beta3' # CHANGE TO '5.0.0.beta2' for tests to pass
    gem 'sqlite3'
    gem 'pry-nav'
    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 :accounts, force: true do |t|
    end

    create_table :authorizations, force: true do |t|
    t.integer :authority_id, null: false
    t.string :authority_type, null: false
    end
    end

    class Account < ActiveRecord::Base
    end

    class Authorization < ActiveRecord::Base
    validates :authority_id, :authority_type, presence: true
    belongs_to :authority, polymorphic: true
    end

    class Partnership < Authorization
    validates_associated :authority
    end

    class BugTest < Minitest::Test
    def test_polymorphic_validates_associated
    ActiveRecord::Base.belongs_to_required_by_default = true

    account = Account.create!
    account = Account.create!
    authority = Authorization.create! authority: account
    partnership = Partnership.new authority: authority

    Authorization.belongs_to :partner, foreign_key: 'authority_id', class_name: 'Authorization'

    assert partnership.valid?, partnership.errors.full_messages.to_sentence
    # on Rails 5.0.0.beta2:
    # => No errors

    # on Rails 5.0.0.beta3 (and master@364e155):
    # => Failure: Authority is invalid

    end
    end