rails: ActiveRecord::Base#dup does not include dynamic attributes
Steps to reproduce
# give MyModel which simply has id:pk, field:string, and timestamp fields
records = MyModel.group(:field).select(:field, Arel.sql('count(*) as total'))
record = records.first
puts record.to_json
# output: {"id":null,"field":"one", "total": 1}
n = record.dup
puts n.to_json
# output: {"id":null,"field: "one","created_at":null,"updated_at":null}
Expected behavior
in Rails 4.2.x duplicating the record would retain/dup the “selected” attributes Thus the second puts would output
puts n.to_json
# output: {"id":null,"field":"one", "total": 1}
Actual behavior
lib/active_record/core.rb#initialize_dup correctly duplicates the @attributes
(as it did in Rails 4.2.x)
However
lib/active_model/dirty.rb#initialize_dup is obliterating the already duplicated by only pulling in fields defined in the MyModel._default_attributes
System configuration
Rails version: 5.2.2 Ruby version: 2.4.3
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 19 (13 by maintainers)
This is still an issue.