Updating values under mass_assign_any_attribute

Sett 9 ganger
Hopp til første uleste melding

Sam Wilder

ulest,
5. okt. 2011, 04:54:3905.10.2011
til 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

ulest,
5. okt. 2011, 12:47:4105.10.2011
til 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
Svar alle
Svar til forfatter
Videresend
0 nye meldinger