Skip to content

Instantly share code, notes, and snippets.

@tehprofessor
Last active December 18, 2015 12:39
Show Gist options
  • Save tehprofessor/5784603 to your computer and use it in GitHub Desktop.
Save tehprofessor/5784603 to your computer and use it in GitHub Desktop.
DataMapper won't save/update object if called in #each block I'm sure I'm doing something übern00b here (as I am a DM n00b).
instances = Model.all
# If I try to perform this on all the instances, all return true but the changes are not persisted.
instances.each do |instance|
desc = instance.description
desc.gsub!(/some\swords/, '')
instance.update(:description => desc) # doesn't work
instance.description = desc
instance.save # doesn't work and returns true.
end
# Now if I go look through the instances with another call to #all and put the description nothing has changed.
Model.all.each{|model| puts model.description }
# This works for changing the description and persists it, but only if I don't call it in the #each block
# on a collection... ID = a valid integer id.
desc = Model.get(ID).description
m = Model.get(ID)
desc.gsub!(/some\swords/, '')
Model.get(ID).update(:description => desc)
# OR #
m.description = desc
m.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment