开发者

Ruby on Rails Query Count

I'm developing a site where users can post opinions, as well as rate opinion up or down.

In turn, I need to find a way to count opinion that have been rated up or down.

I'm currently working on those which have been rated up. I have a many-to-many relationship between the following entities: Opinion and Rating.

The two entities are joined together by a table called OpinionRatings. Below is the query I have come up with so far.

@topUpSize = OpinionRating.find(
      :first,
      :select => "count(opinion_ratings.opinion_id) as count",
      :joins => "inner join ratings on opinion_ratings.rating_id开发者_运维问答=ratings.id",
      :group => "opinion_ratings.opinion_id",
      :having => ["ratings.up=?", true]
    )

The problem I have is I currently have two separate test opinions that have been rated up so the count that should be displayed is two, however, the count that is displayed is one. I'm not sure why this is happening.

Any help would be appreciated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜