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.
> 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.
>
> 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.
>
> 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?
I'd consider subqueries a code smell. There are some few cases where
they're useful, but 9 times out of 10 you probably really wanted a join.
> I've forced the
> first technique into ActiveRecord wrapping, which isn't fun. Far from
> being any help. ARec gets in the way.
In this case, AR is getting in the way of something you shouldn't have
to be doing anyway. There's no need to outsmart it here.
> I've dabbled with ARel which was
> even worse, but may be due to my inexperience with it.
Likely. I've never used Arel, but my impression from the docs is that
it could be quite helpful in this sort of case.
>
> Currently, the nicest solution I can think of is to accept the SQL and
> go native: i.e., define a view in the database and a read-only model on
> top of it.
You could do that, but it seems like overkill. See above for more
reasonable solutions.
>
> I appreciate any suggestions how to do this *elegantly* in ARec.
Stop overthinking. Stop fighting Rails. Stop trying to use such a poor
data model.
>
> Michael
>
>
> [*] 2010-06-20, ActiveRecord and ARel: correlated subqueries?
> --
> Michael Schuerig
> mailto:michael@schuerig.de
> http://www.schuerig.de/michael/
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Sent from my iPhone
--
Posted via http://www.ruby-forum.com/.
--
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.
No comments:
Post a Comment