2010-06-29 4 views
0

Iam utilisant l'esprit SQLite une vue comme celui-ci:Création des migrations pour les vues SQL dans les rails avec des valeurs booléennes

CREATE VIEW view_importaciones AS 
     SELECT fecha_importacion, COUNT(DISTINCT(total)) - 1 AS total, COUNT(DISTINCT(errores)) -1 AS errores, estado FROM 
     (
      SELECT fecha_importacion, id AS total, 0 as errores, estado FROM marcas WHERE parent_id = 0 
      UNION 
      SELECT fecha_importacion, 0 AS total, id as errores, estado FROM marcas WHERE valido = 'f' AND parent_id = 0 
     ) AS importaciones GROUP BY fecha_importacion ORDER BY fecha_importacion 

Comme vous pouvez le voir valido = « f » est codé en dur, mais je vais devoir utiliser MySQL à l'avenir, je lance cette requête en utilisant la méthode d'exécution comment puis-je créer la requête correcte pour chaque adaptateur "mysql, sqlite, postgresql, etc." pour créer la vue SQL.

Répondre

0

J'ai trouvé la solution:

# First create the sql needed with the parameters, this way 
# the query will change depeding on the database 
sql = Marca.send(:construct_finder_sql, 
        :select => "fecha_importacion, 0 AS total, id AS errores, estado", 
        :conditions => { :valido => false, :parent_id => 0} 
        :group => "marcas.fecha_importacion" 
       ) 

# Add the sql where needed 
sql = "CREATE VIEW view_importaciones AS 
    SELECT fecha_importacion, COUNT(DISTINCT(total)) - 1 AS total, COUNT(DISTINCT(errores)) -1 AS errores, estado FROM 
    (
     SELECT fecha_importacion, id AS total, 0 as errores, estado FROM marcas WHERE parent_id = 0 
     UNION 
     #{sql} 
    ) AS importaciones GROUP BY fecha_importacion ORDER BY fecha_importacion" 
# Run the sql 
execute(sql) 
Questions connexes