Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Eager loaded belongs_to associations are always readonly
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', github: 'rails/rails'
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 :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_join_doesnt_make_readonly
post = Post.create!
post.comments << Comment.create!
comment = Comment.where(id: "1").joins(:post).first!
assert !comment.post.readonly?
end
def test_eager_load_doesnt_make_readonly
post = Post.create!
post.comments << Comment.create!
comment = Comment.where(id: "1").eager_load(:post).first!
assert !comment.post.readonly?
end
end
=begin
Output:
1) Failure:
BugTest#test_eager_load_doesnt_make_readonly [active_record_master.rb:55]:
Failed assertion, no message given.
2 runs, 2 assertions, 1 failures, 0 errors, 0 skips
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.