Skip to content

Commit 5b3607d

Browse files
Filter unknown attributes in assign_attributes when raise_on_unknown_attributes is false
- Modify assign_attributes to check raise_on_unknown_attributes setting - Filter out unknown attributes before calling super when disabled - Log warning for ignored attributes
1 parent 9e9ecfd commit 5b3607d

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

lib/couchbase-orm/persistence.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,18 @@ def update_attribute(name, value)
155155

156156
def assign_attributes(hash)
157157
hash = hash.with_indifferent_access if hash.is_a?(Hash)
158-
super(hash.except("type"))
158+
159+
# Filter unknown attributes if raise_on_unknown_attributes is false
160+
if !self.class.raise_on_unknown_attributes
161+
known_attrs = hash.slice(*self.class.attribute_names).except("type")
162+
unknown_attrs = hash.keys - self.class.attribute_names - ["type"]
163+
if unknown_attrs.any?
164+
CouchbaseOrm.logger.warn "Ignoring unknown attribute(s) for #{self.class.name}: #{unknown_attrs.join(', ')}"
165+
end
166+
super(known_attrs)
167+
else
168+
super(hash.except("type"))
169+
end
159170
end
160171

161172
# Updates the attributes of the model from the passed-in hash and saves the

0 commit comments

Comments
 (0)