如何只检索受欢迎的用户?

时间:2014-07-01 10:06:05

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

我正在使用名为'acts_as_votable'的宝石(https://github.com/ryanto/acts_as_votable) 现在,我正在尝试获取代码的特定记录。

  1. 代码:belongs_to用户&社区
  2. 社区:has_many代码
  3. 用户:has_many代码

    #fetching all the communities that current_user voted up.
    @communities = current_user.get_up_voted(Community)
    
    #Now I'm trying to retrieve all users who belongs to any of '@communities'
    @users = @communities.???????(User)
    @codes = users.codes
    
  4. 如何编码以检索代码?

1 个答案:

答案 0 :(得分:1)

当您从所希望的社区中采样所有代码时,您可以从表用户中选择所需的条目。尝试:

@codes  = []
@communities.each do |community|
  @codes.concat(community.codes)
end
User.where(code: @codes).order(:active_at)

另见: http://guides.rubyonrails.org/active_record_querying.html