Skip to content

Instantly share code, notes, and snippets.

@dskecse
Created June 15, 2017 11:43
Show Gist options
  • Save dskecse/e6d0bdafa9b33f8877e4bd5cac7e9cc8 to your computer and use it in GitHub Desktop.
Save dskecse/e6d0bdafa9b33f8877e4bd5cac7e9cc8 to your computer and use it in GitHub Desktop.

Revisions

  1. dskecse created this gist Jun 15, 2017.
    56 changes: 56 additions & 0 deletions activerecord_proxy_association_bug.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    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'
    # Activate the gem you are reporting the issue against.
    gem 'activerecord', '5.0.3'
    gem 'sqlite3'
    end

    require 'active_record'
    require 'minitest/autorun'
    require 'logger'

    # Ensure backward compatibility with Minitest 4
    Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

    # 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 :articles, force: true

    create_table :comments, force: true do |t|
    t.text :content
    t.references :article, foreign_key: true
    end
    end

    class Article < ActiveRecord::Base
    has_many :comments do
    def build(*args, &block)
    args << { content: 'Sample' }
    super
    end
    end
    end

    class Comment < ActiveRecord::Base
    belongs_to :article
    end

    class BugTest < Minitest::Test
    def test_association_stuff
    article = Article.create!
    comment = article.comments.build

    assert_equal comment, article.comments.first
    assert_equal 'Sample', article.comments.first.content
    end
    end