Skip to content

Commit 3c3aec4

Browse files
committed
Filter virtual columns from the attribute names
1 parent bac0038 commit 3c3aec4

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,20 +387,22 @@ def attributes_with_values(attribute_names)
387387
attribute_names.index_with { |name| @attributes[name] }
388388
end
389389

390-
# Filters the primary keys and readonly attributes from the attribute names.
390+
# Filters the primary keys, readonly attributes and virtual columns from the attribute names.
391391
def attributes_for_update(attribute_names)
392392
attribute_names &= self.class.column_names
393393
attribute_names.delete_if do |name|
394-
self.class.readonly_attribute?(name)
394+
self.class.readonly_attribute?(name) ||
395+
column_for_attribute(name).virtual?
395396
end
396397
end
397398

398-
# Filters out the primary keys, from the attribute names, when the primary
399+
# Filters out the virtual columns and also primary keys, from the attribute names, when the primary
399400
# key is to be generated (e.g. the id attribute has no value).
400401
def attributes_for_create(attribute_names)
401402
attribute_names &= self.class.column_names
402403
attribute_names.delete_if do |name|
403-
pk_attribute?(name) && id.nil?
404+
(pk_attribute?(name) && id.nil?) ||
405+
column_for_attribute(name).virtual?
404406
end
405407
end
406408

0 commit comments

Comments
 (0)