I have a question about good ActiveRecord style. I have a model Page
with two attributes, named “title” and “body”. The title is never set by
the user directly, but is extracted from the body, which includes an
HTML structure like the following:
The page model should automatically extract the content from the H1
element(s) and store them in the attr_protected title attribute.
At the moment I do this the following way:
self.title = detect_title
This works great so far. But I remarked that this way the data in any
Page object will only be valid after the call of valid? or safe (or any
other such related methods). So before doing this, it’s possible that I
have invalid data in a Page object!
My question: is this OK? Or should I overwrite the body=() method or
something to make the “magic” happen as soon as the body itself changes?
Or is it agreed that modified AR objects can have temporary invalidities
as long as they’re not safed/validated?
Thanks a lot for help