I take issue with the faxt that in some cases ActiveRecord may
save records using “save” rather than “save!”. Specifically, this is
assigning an object to a “has_one” relation, or adding an object to the
collection vreated by the remaining “has_*” associations.
The automatic saving alone seems a bit questionable to me, but I’ll
that second. The fact that it uses “save” rather than “save!” is a
Let me take the example a a “Bar” class that “has_one :foo”. If i use
Bar#foo=, if the foo fails validation I get a silent failure.
The only way to check for failure is to check for the return value of
assignment. That is really ugly. First of all that is very unexpected.
Nobody whould ever just guess that one needs to check the return value
assignment. Further the idiom of checking the return value of an
already exists in other languages where it is largly just a shorthand.
people familar with other languages would misinterpet the code.
It is also a very easy idiom to forget. People are very accustomed to
assignments never failing without throwing an exception. Overall this
thing severly violates the priciple of least surprise.
There is a similar case with the remainin has_* associations, but the
problem is slightly less severe there, as at least the idioms needed
(checking the return value of << or of Baz#foos.push, etc) are less
surprising. But it is still easy to forget to check in the first place.
I would really like it if there were some way to request that “save!” be
used in place of “save” in these locations. I’m not sure how best to
such an option, since existing code that expects the current behavior
broken by such a change. (Although I suspect there is much more code
currenly silently failing to save in some cases, where throwing an
would help to higlight the brokeness of the code.)
But I’m not entirely convinced that saving immediately is the correct
to do. Why not just delay the saving until the object with the “has_"
association is also saved? The code nessisary to do so already exists
because that is absolutely required in the case that the object with the
"has_” association is a new record. So why not just do that in all
Am I just missing something here?