Ruby on rails - 如何加入4个表?

时间:2018-02-12 02:36:08

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

这就是我所拥有的:

class Membership < ApplicationRecord
  belongs_to :user
  belongs_to :group
end

class User < ApplicationRecord
  has_one :user_score
  has_many :memberships
  has_and_belongs_to_many :groups, through: :memberships # an user can be owner or member of a group
end

class Group < ApplicationRecord
  has_many :memberships
  has_many :members, through: :memberships, source: :user
end

class UserScore < ApplicationRecord
  belongs_to :user
end

我要做的是从“total_score”(这是UserScore的一个属性)订购的组中获取用户列表,但到目前为止我都失败了。

1 个答案:

答案 0 :(得分:0)

显然,我没有在我的机器上加载架构。所以,我的回答将是

User.joins(:user_score, :groups).where(groups: {name: 'Name of your group'}).order('user_scores.total_score')