2

I have this query:

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 

How do I write it in Rails? Right now I build an array with a string, but is there a rails way to do it better? If there is no better way, just mention it.

The values in the line: WHERE tag_id IN( 76, 72, 74 ) are being created dynamically.

4

1 に答える 1

1

私は複雑な SQL を Rails に統合しようとしてきましたが、SQL を直接実行するよりも良い方法は見つかりませんでした。非常に単純なクエリから逸脱すると、activerecord でコーディング (および理解) するのが非常に難しくなる可能性があります。

于 2013-06-06T15:32:54.473 に答える