Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created June 22, 2014 03:01
Show Gist options
  • Save jturkel/7e39d519f9e017d4e4b5 to your computer and use it in GitHub Desktop.
Save jturkel/7e39d519f9e017d4e4b5 to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Schema
ActiveRecord::Schema.define do
ActiveRecord::Base.connection.create_table(:posts, force: true) do |t|
t.integer :author_id
end
ActiveRecord::Base.connection.create_table(:authors, force: true)
end
# Models
class Post < ActiveRecord::Base
belongs_to :author
belongs_to :read_only_author, -> { readonly }, class_name: 'Author', foreign_key: :author_id
end
class Author < ActiveRecord::Base
has_many :posts
end
class BugTest < Minitest::Test
def test_eager_load_read_only_association
Post.create!(author: Author.create!)
post = Post.includes(:read_only_author).first!
assert_raises(ActiveRecord::ReadOnlyRecord) { post.read_only_author.save! }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment