On Friday 03 September 2010, Marnen Laibow-Koser wrote:
> Michael Schuerig wrote:
> > Firstly, I've asked a similar question before[*]. I didn't get any
> > answers back then, maybe now I have better luck
> > 
> > Let's say I have models like this
> > 
> >   class Article < ActiveRecord::Base
> >   
> >     has_many :versions
> >   
> >   end
> >   
> >   class Version < ActiveRecord::Base
> >   
> >     belongs_to :article
> >     default_scope order('updated_at')
> >     scope :published, where(:state => 'published')
> >     validates :state, :inclusion => { :in => ['draft', 'published']
> >     } validates :title, :content, :presence => true
> >   
> >   end
> 
> Side question: why aren't you using one of the existing version
> plugins? It's possible that this problem has already been solved.

Because none of them fit, last time I looked. I need to interact with 
several versions at the same time.

> > It's easy to find the latest version for an article, just
> > 
> >   article.versions.last
> > 
> > The same for published versions isn't much more complicated
> > 
> >   articles.versions.published.last
> 
> Your data model is smelly.  You should probably have the version
> metadata in the Article record, not in a separate table.  With your
> model, all your Article data fields are really in the Version object.
> That's cumbersome, as you're finding out.

The data model expresses what I need. Versions of the same article can 
have different titles. It is likely that an older version of an article 
is already published whereas the latest version is still in draft state.

> > Of course, I'm not only dealing with single articles
> > 
> >   Article.all
> > 
> > Then, in a articles/_article.html.erb
> > 
> >   <%= article.version.last.title %>
> > 
> > Oops! That triggers another database access for each article.
> 
> Not if you had done Article.all :joins => :versions in the first
> place.

:joins with a symbol does an inner join. When there are several draft 
versions, which one would I get? Answer: any. I want a specific one: the 
latest.

> > Now, I know how to deal with this in SQL, with either a correlated
> > sub- select or an even more complicated left outer join.
> 
> No way!  A simple join should do it.  Why do you think you need
> anything more complex?

Before you claim that I'm wrong, please consider the possibility that 
you don't understand what I'm trying to do. Thank you.

> Stop overthinking.  Stop fighting Rails.  Stop trying to use such a
> poor data model.

Stop being overly confident of your opinion when you don't understand 
the situation. If you think my problem statement was not precise enough, 
you could have asked for the details you thought were missing. No need 
to start out with bold and unwarranted statements.

Michael

-- 
Michael Schuerig
mailto:mich...@schuerig.de
http://www.schuerig.de/michael/

-- 
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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to