我有一个User模型和一个PullRequest模型 - 用户有很多PullRequests,PullRequest属于User。我有一个计数器列用于跟踪用户的PullRequests总数 - 但我希望能够找到用户在指定日期之后有多少。我是这样做的:
$("#Schedule").html(data);
但是如何使用它来对拉取请求控制器中的数据进行排序?
目前我有 has_many :pull_requests_as_creator_sprint, -> {where("merged_at >= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
以总创建的数量显示 - 但我想按照自
创建的顺序进行排序@team_members = User.order(sort_column + " " + sort_direction, 'pull_requests_created_count DESC').includes(:pull_requests_as_merger_sprint, :pull_requests_as_creator_sprint)
我的用户模型 - :pull_requests_as_creator / merger关系很好,他们只使用计数器缓存。但我希望能够在日期之后通过pull_requests对数据进行排序。
has_many :pull_requests_as_creator, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
has_many :pull_requests_as_creator_sprint, -> {where("merged_at >= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'creator_id', dependent: :destroy
has_many :pull_requests_as_merger, class_name:'PullRequest', foreign_key: 'merger_id', dependent: :destroy
has_many :pull_requests_as_merger_sprint, -> {where("merged_at >= ?", "2015-10-22")}, class_name:'PullRequest', foreign_key: 'merger_id', dependent: :destroy
是我的用户模型。
答案 0 :(得分:0)
这样的事情应该有效:
User.select('users.id, count(pull_requests.id) AS pull_requests_count').joins(:pull_requests).where('pull_requests.created_at >= ?', your_date_here).group('users.id').order('pull_requests_count')
你可以把它变成一个范围,这样你就可以更容易地访问它。
User.select('users.id, count(pull_requests.id) AS pull_requests_count').joins(:pull_requests_as_merger).where('pull_requests.created_at >= ?', your_date_here).group('users.id').order('pull_requests_count')
或
User.select('users.id, count(pull_requests.id) AS pull_requests_count').joins(:pull_requests_as_merger).where('pull_requests_as_merger.created_at >= ?', your_date_here).group('users.id').order('pull_requests_count')