0

私はモデルを持っUserていCommunityます。

Communityモデルは、このコマンドによってユーザーに好かれます

    if current_user.voted_up_on? @community
        current_user.dislikes @community
    else
        current_user.likes @community
    end

@community を気に入ったすべてのユーザーを取得しようとしています。私はこれでそれをやろうとしています。ただし、正しいユーザーは返されません。
どうすればこれを修正できますか?

@users =  User.where(:id => @community.likes.map(&:voter_id)).order("last_active_at DESC").limit(10)

このhttps://github.com/ryanto/acts_as_votableには「acts_as_votable」という宝石を使用してい ます

4

2 に答える 2

1

これを試して:

@users = @community.likes.map(&:voter)

于 2013-02-03T09:19:17.310 に答える
1

それは AR::Relation を返します

# Note: I'm not sure that polymorphic fields are: "votable_type" and "votable_id"

class Community
  ...
  def last_liked(limit = 10)
    User.includes(:votes).where(["votes.votable_type = ? AND votes.votable_id = ?", self.class.name, self.id]).order("users.last_active_at DESC").limit(limit)
  end
end
于 2013-02-03T15:44:27.547 に答える