0

I have a Post model that is using Twitter's activerecord reputation system to track votes:

Post model:

has_reputation :votes, source: :user, aggregated_by: :sum

I am attempting to eager load the reputation associate with all posts on an index page. Following this, I'm trying to do so with:

Post.find_with_reputation(:votes, :all, { :conditions => ["post_id = ?", self.id] })

Doing this, I get the following error:

undefined method `id' for #<PostsController:0x007f8560c88150>

If I simplify to:

Post.find_with_reputation(:votes, :all)

I get the following error in my view:

undefined method `votes' for #<Post:0x007f8560a6b610>

Any direction on where I'm going wrong here?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
dmt2989
  • 1,610
  • 3
  • 17
  • 30

1 Answers1

0

you can use in this way ....

@all_videos = Video.includes(:user,:reputations,:tags,:comments).where("videos.user_id !=?",current_or_guest_user.id).paginate(:page =>params[:page], :per_page => 10).find_with_reputation(:views, :all, order: "views desc")

includes help out for eager loading using ActiveRecordRelation

Milind
  • 4,535
  • 2
  • 26
  • 58
  • Thanks for the suggestion. I tried a slightly modified version of this:
    @postsswithrep = Post.includes(:user,:reputations).where("posts.user_id !=?",current_user.id).find_with_reputation(:votes, :all, order: "votes desc") but unfortunately this still gives me "undefined method votes.." when calling @postwithrep[0].votes
    – dmt2989 Aug 12 '14 at 16:16
  • @dmt2989..try changing to this `has_many :evaluations, class_name: "RSEvaluation", as: :source has_reputation :votes, source: {reputation: :votes, of: :self}, aggregated_by: :sum` – Milind Aug 18 '14 at 13:19
  • @Milind- unfortunately this still results in "undefined method 'votes'" when I attempt to call post.votes in the view – dmt2989 Aug 19 '14 at 23:42
  • 1
    @dmt2989...i think you have not configured the activerecord_reputation gem properly..please go through http://railscasts.com/episodes/364-active-record-reputation-system and tell me that you have followed the same. – Milind Aug 20 '14 at 09:08