# May AR objects contain "invalid" data when not saved yet?

**URL:** <https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095>\
**Category:** Rails\
**Created:** [October 11, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095 "2009-10-11T15:38:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 3:38pm UTC](https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095/1 "2009-10-11T15:38:38Z")

</div>

Hi all

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:

# heading 1.1

content

## heading 2.1

content 2

## heading 2.2

content 2

...etc...

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:

…  
def before\_validation  
self.title = detect\_title  
end  
private  
def detect\_title  
Nokogiri::HTML(self.body).xpath(’//h1’).collect(&:content).to\_sentence  
end  
…

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 🙂  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 11, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095/2 "2009-10-11T17:00:54Z")

</div>

2009/10/11 Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ## heading 2.1
> ... 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?

If the title is always determined by the contents of other fields then  
arguably you should not store it at all as then you have redundant  
data in the db. Just provide a method called title that calculates it  
when required. If you want to store it for reasons of efficiency then  
leave it till efficiency becomes an issue.

Colin

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 12, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095/3 "2009-10-12T16:56:29Z")

</div>

On Oct 11, 9:38 am, Joshua M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

While generally I agree with the idea of “don’t optimize prematurely”,  
there’s a line between that idea and “don’t write clearly slow code”.  
I think overriding body= here is a good idea, since the  
before\_validation callback will fire every time the record is saved,  
even if body hasn’t changed. Normally that’s OK, but instantiating a  
Nokogiri parser is quite a bit heavier than the typical validation  
action…

–Matt

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 12, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/may-ar-objects-contain-invalid-data-when-not-saved-yet/177095/4 "2009-10-12T17:12:17Z")

</div>

2009/10/12 Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 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?
> 
> While generally I agree with the idea of “don’t optimize prematurely”,  
> there’s a line between that idea and “don’t write clearly slow code”.

It is possible I skimmed the question rather too quickly and did not  
notice the complexities of determining the title.

Colin
