【问题标题】:How can I fetch all the users who is liking the Community when using acts_as_votable?使用acts_as_votable时如何获取所有喜欢社区的用户?
【发布时间】:2013-01-05 18:15:50
【问题描述】:

我在 communities_controller.rb中有 'like' 操作
如果我想获取当前所有喜欢社区的用户怎么办,我该如何在控制器中写入? 我正在使用名为 acts_as_votable

的 gem

应该是这样的吧?

@community = Community.find(params[:id])
@users = User.likes @community

communities_controller.rb

def like
    if current_user.voted_up_on? @community
        current_user.dislikes @community
        flash[:notice] = "Deleted from favorite"
    else
        current_user.likes @community
        flash[:notice] = "Added to favorite"
    end

    respond_to do |format|
        format.html { redirect_to @community }
    end
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    要获得所有喜欢社区的用户,您可以编写

    @community.likes
    

    查看acts_as_votable Documentation 以了解您执行的所有其他操作。

    【讨论】:

    • 谢谢,但这会返回错误。似乎没有吸引用户.. @community.likes.order("last_signed_in_at DESC").limit(3).each do |user|
    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多