简体   繁体   English

通过named_scope返回对象数组 - has_many ... belongs_to association; UNION ALL查询

[英]Returning array of objects via named_scope — has_many…belongs_to association; UNION ALL query

I'm looking for an answer that will return an array of user objects via (preferably) a named_scope or via a class method on the User model that does some manipulation. 我正在寻找一个答案,它将通过(最好)一个named_scope返回一个用户对象数组,或者通过User模型上的类方法返回一些操作。

So without further ado... 所以没有进一步的...

I have two tables: users and fights. 我有两个表:用户和战斗。

  • User has many fights (has_many :fights, :foreign_key => 'challenger_id or challengee_id') 用户有很多打架(has_many:打架,:foreign_key =>'challenger_id或challengee_id')
  • Fight belongs to user (belongs_to :challenger, :class_name => 'User'...belongs_to :challengee, :class_name => 'User') 战斗属于用户(belongs_to:challenger,:class_name =>'User'... belongs_to:challengee,:class_name =>'User')

Fight has the following columns of concern: 战斗有以下几个关注的栏目:

  • challenger_id (user_id fk) challenger_id(user_id fk)
  • challengee_id (user_id fk) challengee_id(user_id fk)
  • challenger_won (boolean) challenger_won(布尔)

As you can see, a user can be a challenger or a challengee, but not both. 如您所见,用户可以是挑战者或挑战者,但不是两者。

  • If the user is a challenger and the challenger_won = true, then it is considered a win. 如果用户是挑战者并且challenger_won = true,那么它被认为是胜利。
  • If the user is a challengee and the challenger_won = false, then it is considered a win. 如果用户是挑战者且challenger_won = false,则认为是胜利。
  • If the challenger_won = null, then just disregard it. 如果challenger_won = null,那么就忽略它。

I have a raw SQL statement that returns a fighter attribute (the user_id) grouped by most wins attribute: 我有一个原始的SQL语句,它返回按最多wins属性分组的战斗机属性(user_id):

SELECT a.fighter, COUNT(*) AS wins
  FROM (SELECT challenger_id AS fighter
          FROM fights
         WHERE challenger_won = TRUE
        UNION ALL
        SELECT challengee_id AS fighter
          FROM fights
         WHERE challenger_won = FALSE
       ) AS a
 GROUP BY a.fighter;

So given this info, how can I return an array of user objects via (preferably) a named_scope or via a class method on the User model that does some manipulation? 所以给定这个信息,如何通过(最好)一个named_scope返回一个用户对象数组,或者通过用户模型上的类方法进行一些操作?

I think you can try something like this to recreate the result of you query: 我想你可以尝试这样的东西来重新创建你的查询结果:

class User
    named_scope :winners, 
        :select => 'users.*, COUNT(fight.id) AS wins', 
        :join => :fights,  
        :conditions => ['(fights.challenger_id = user_id AND fights.challenger_won = TRUE) OR (fights.challengee_id = user_id AND NOT fights.challenger_won = FALSE)']
        :group => 'user_id'
end

However, for caching puposes, you might want to consider adding a win_count field to the User model. 但是,对于缓存目的,您可能需要考虑将win_count字段添加到User模型。 If you create a setter method for the winner of a fight, you can increment the win_count right at that moment when it changes. 如果为战斗的胜利者创建一个setter方法,则可以在更改时立即增加win_count。

Or, without the join (mysql specific) 或者,没有连接(特定于mysql)

class User
  named_scope :winners, 
    :select => 'if(challenger_won = FALSE,challenger,challengee) as winner,COUNT(id) AS wins ', 
    :group => 'user_id'
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为此逻辑编写一个SQL查询(has_many,belongs_to关联)? - How to write a SQL query for this logic (has_many, belongs_to association)? 查询两个表之间的belongs_to / has_many关系 - Query on two tables with belongs_to/has_many relation Rails 4“where” - 查询“has_many belongs_to” - 关系,搜索特定计数 - Rails 4 “where”-query with “has_many belongs_to”-relationship, searching with specific count Rails-SQL查询返回所有具有所有指定相关对象的对象(has_many至) - Rails - SQL query returning all objects, which have all specified related objects (has_many through) Ruby on rails-使用JUST'belongs_to'与使用BOTH'has_many'和'belongs_to'之间的区别? - ruby on rails - difference between using JUST 'belongs_to' versus using BOTH 'has_many' and 'belongs_to'? 通过has_many关系基于多个对象的ActiveRecord查询 - ActiveRecord query based on multiple objects via has_many relationship 范围/ named_scope中的多联接查询 - Multiple join query in a scope/named_scope Rails SQL查询通过has_many通过以下方式查找属于两个不同用户的记录 - Rails sql query to find record that belongs to two different users via has_many through 如何将SQL查询编写为named_scope? - How to write SQL query as named_scope? 如何使用DBIx :: Class创建嵌套的has_many或belongs_to关系? - How do I create a nested has_many or belongs_to relationship with DBIx::Class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM