twitter / activerecord-reputation-system

An Active Record Reputation System for Rails
Apache License 2.0
1.33k stars 122 forks source link

Eager loading reputation value in nested eager loading hash. #64

Open chrisadams opened 11 years ago

chrisadams commented 11 years ago

Hi, first I just want to thank you for all the hard work you're doing and have done in creating and supporting this gem.

I have a question that's almost exactly the same as this one.

In the aforementioned issue, you explained that this:

@comment = Comment.find_with_reputation(:reputation_name, :all)

would allow you to access the reputation value like this:

@comment.comment_votes

without additional DB queries.

You said that code like this:

Comment.includes(:reputations)

would not give you the same ability to access the reputation value without another query.

Like the author of the previous issue, I want to eagerly load reputation value, but in my case I want to do so inside of a nested eager loading hash.

My unoptimized code looks like this:

@article = Article.find(1)
@comments = @article.comments.find_with_reputation(:votes, :all, :order => 'votes desc', :limit => 20)

I want to write something with eager loading like this:

@article = Article.includes(comments: :comment_votes).find(1)

and then be able to access comments[0].comment_votes.

So my question is: Is there anyway to eagerly load comment_votes in the way that find_with_reputation does inside of a nested eager loading hash (i.e. Article.includes(comments: :comment_votes) )?

And if there is a way, is it also possible to include conditions on the comments?

Sorry if this is a silly question, as I'm pretty new to Rails, and thanks for any help!

archonic commented 9 years ago

On the subject of #27, I'm still hoping for a way to eager load two reputation associations, as well evaluators.

I'm trying to eager load 2 reputation names and evaluators for a collection that's already fairly complex. When I try to use find_with_reputation, I get undefined method find_with_reputation for #<Array:0x0000001ad17df0>. If I call relation on the AssociationProxy instance, I get undefined method relation for #<ActiveRecord::Relation:0x0000001c6a2018>.

I really like how the associations are built in this gem, but lack of eager loading is really holding back search result pages in my app.