Sunday, October 11, 2009

[Rails] Re: May AR objects contain "invalid" data when not saved yet?

2009/10/11 Joshua Muheim <rails-mailing-list@andreas-s.net>:
>
> 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:
>
> <h1>heading 1.1</h1>
> <p>content</p>
> <h2>heading 2.1</h1>
> <p>content 2</p>
> <h2>heading 2.2</h1>
> <p>content 2</p>
> ...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?

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

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate