Skip to content

Instantly share code, notes, and snippets.

@bquorning
Last active April 20, 2016 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bquorning/55dffab8e8038fbe0119ef6d854a5090 to your computer and use it in GitHub Desktop.
Save bquorning/55dffab8e8038fbe0119ef6d854a5090 to your computer and use it in GitHub Desktop.
ActiveRecord trying to save readonly records on has_many_through assignment. It looks like the bug was introduced by https://github.com/rails/rails/commit/d849f42b4ecf687ed5350f5a2402fb795aa33aac, and it should probably check `if !record.readonly?` and not just remove the condition.
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'activerecord', '5.0.0.beta3'
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 :clubs, force: true
create_table :memberships, force: true do |t|
t.integer :member_id
t.integer :club_id
end
create_table :members, force: true
end
class Club < ActiveRecord::Base
has_many :memberships
has_many :members, through: :memberships
end
class Membership < ActiveRecord::Base
belongs_to :member
belongs_to :club
end
class Member < ActiveRecord::Base
def readonly?
persisted?
end
end
class BugTest < Minitest::Test
def test_assign_attributes
club = Club.create!
member = Member.create!
club.assign_attributes(member_ids: [member.id])
assert_equal [member], club.members
end
def test_collection_shovel
club = Club.create!
member = Member.create!
club.members << member
assert_equal [member], club.members
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment