This is not a problem of <<
not modifying the string in-place - it does modify the string in-place. <<
might not have a !
in its name, but it still acts like a bang-method. You can see this easily if you replace <<
with a call to gsub!
(which does have a !
in its name) in your example code: it won't make a bit of difference.
In your case you get back the unchanged string because widget.notes.where(:author_id => a).first presumably returns a new object each time, which will have its own independent string.
Further, if message
corresponds to a database column, not an instance variable, I'm not sure whether calling mutating methods on it will even work as it would circumvent calling ActiveRecord's (or whichever ORM you're using) setter methods. Using +=
might be your safest bet here.
This should work as you want:
note = widget.notes.where(:author_id => a).first
note.message += "Potato"
note.save