[英]Ruby on rails - how to join 4 tables?
This is what I have: 这就是我所拥有的:
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
What I'm trying to do is to get a list of users from a group ordered by 'total_score' (which is an attribute of UserScore) but I've failing so far. 我想做的是从按“ total_score”(这是UserScore的属性)排序的组中获取用户列表,但是到目前为止,我还是失败了。
Obviously, I do not have the schema loaded on my machine. 显然,我的计算机上没有加载架构。 So, my answer will be something like
所以,我的回答将是
User.joins(:user_score, :groups).where(groups: {name: 'Name of your group'}).order('user_scores.total_score')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.