Updating values under mass_assign_any_attribute

9 views
Skip to first unread message

Sam Wilder

unread,
Oct 5, 2011, 4:54:39 AM10/5/11
to CouchRest
Hello,
I have set mass_assign_any_attribute = true
I am able to add fields to document which are not defined as
properties using update_attributes.
However when i try to update these fields using update_attributes,
they are not getting updated.

Say for e.g. I have
class Person < CouchRest::Model::Base
property :name, String
end

Say for,
p = Person.first
p.update_attributes({"name" => "new_Sam", "email" => "n...@email.com"})

This will update name but now the email field, if that was already
assigned a value.

Sam.

Sam Wilder

unread,
Oct 5, 2011, 12:47:41 PM10/5/11
to CouchRest
Did some more investigation. Sounds like this was intended in the
codebase. However it us a good feature to have.
A clarification the updates does happen if one of the properties is
changed as well as changed_attributes hash does have value then. Thus
in above case if name is changed as well, then change goes through,
otherwise if only email is changed, update does not go through.

I did the following modification, to allow for updates on non-
properties as well. I add the line in couchrest_model-1.1.2/lib/
couchrest/model/properties.rb [line: 113]

def directly_set_attributes(hash, mass_assign = false)
return if hash.nil?
hash.reject do |key, value|
if self.respond_to?("#{key}=")
self.send("#{key}=", value)
elsif mass_assign || mass_assign_any_attribute
Add Here=> couchrest_attribute_will_change!(key) if use_dirty? &&
self[key] != value
self[key] = value
end
end
end

Is this Ok or does it have some unintended side-effects as well.

Sam
Reply all
Reply to author
Forward
0 new messages