rails方式编写复杂的查询

时间:2013-06-06 13:43:10

标签: ruby-on-rails

我有这个问题:

SELECT f.id, 
       Concat(f.name, ' ', REPLACE(f.parent_names, ',', ' ?'))        AS FullName, 
       u.name                                                         AS Unit, 
       u.id                                                           AS UnitId, 
       u.position                                                     AS UnitPosition, 
       city.name                                                      AS City, 
       hus.mobile1                                                    AS HusMobile, 
       wife.mobile1                                                   AS WifeMobile, 
       hus.first_name                                                 AS Spouse1, 
       wife.first_name                                                AS Spouse2, 
       f.phone                                                        AS HomePhone, 
       f.email                                                        AS Email, 
       Date_format(f.contact_initiation_date, '%d/%m/%Y')             AS InitDate, 
       f.contact_initiation_date                                      AS sql_date, 
       Date_format(f.status_change_date, '%d/%m/%Y')            AS StatususChangeDate, 
       stts.name                                                      AS 'Status', 
       (SELECT Group_concat(' ', t.name, '<', t.id, '>' ORDER BY t.position DESC 
               ) 
        FROM   taggings tgs 
               JOIN tags t 
                 ON tgs.tag_id = t.id 
        WHERE  tgs.taggable_type = 'family' 
               AND tgs.taggable_id = (SELECT DISTINCT taggable_id 
                                      FROM   taggings 
                                      WHERE  tag_id IN( 76, 72, 74 ) 
                                             AND taggable_id = f.id)) AS HandlingStatus, 
       Date_format(f.reconnection_date, '%d/%m/%Y')               AS ReconnectionDate, 
       f.reconnection_date                                   AS reconnection_sql_date, 
       Date_format(cmt.created_at, '%d/%m/%Y')                        AS CommentDate, 
       cmt.comment                                                    AS LastComment, 
       usr.name                                                       AS Comentator, 
       Format(e.income_total, 0)                                      AS Income, 
       Format(e.expense_total, 0)                                     AS Expense, 
       Format(e.debts_total, 0)                                       AS Debts 
FROM   families f 
       JOIN categories stts 
         ON f.family_status_cat_id = stts.id 
       JOIN units u 
         ON f.unit_id = u.id 
       JOIN categories city 
         ON f.main_city_cat_id = city.id 
       LEFT JOIN comments cmt 
              ON f.last_comment_id = cmt.id 
       LEFT JOIN contacts hus 
              ON hus.id = f.husband_id 
       LEFT JOIN contacts wife 
              ON wife.id = f.wife_id 
       LEFT JOIN users usr 
              ON usr.id = cmt.user_id 
       LEFT JOIN escort_requests e 
              ON e.family_id = f.id 
WHERE  ( 1 = 0 
          OR ( u.is_busy = 0 
               AND f.family_status_cat_id = 1421 ) 
          OR ( u.is_busy = 1 
               AND f.family_status_cat_id = 1421 ) 
          OR ( f.family_status_cat_id = 1423 ) 
          OR ( f.family_status_cat_id = 1424 ) ) 
HAVING ( 1 = 1 
         AND handlingstatus IS NOT NULL 
         AND fullname LIKE '%%' ) 
ORDER  BY fullname 
LIMIT  0, 100 

如何在Rails中编写它?现在我用字符串构建一个数组,但是有没有更好的做法?如果没有更好的方法,那就提一下。

行中的值:WHERE tag_id IN(76,72,74)是动态创建的。

1 个答案:

答案 0 :(得分:1)

我一直在尝试将一些复杂的SQL与Rails集成,并没有找到比直接运行SQL更好的方法。任何偏离非常简单的查询的东西都很难在activerecord中编码(和理解)。