sql >> Database teknologi >  >> RDS >> Mysql

Hvordan konverteres SQL-forespørgsel til Rails Active Record Query?

For skinner 3, prøv dette:

Training.select("trainings.id, trainings.name, trainings.order, 
                 trainings.id AS history_id, training_histories.finished_at,
                 training_histories.score").
         joins("LEFT OUTER JOIN training_histories 
                ON training_histories.training_id = trainings.id 
                AND training_histories.id = (SELECT th1.id FROM training_histories th1          
                                             WHERE th1.training_id = tc.id
                                             AND th1.finished_at IS NOT NULL
                                             ORDER BY th1.finished_at DESC LIMIT 1)").
         where("trainings.id > 4 AND trainings.id < 8").
         group("trainings.id").
         order("trainings.order_by ASC, trainings.id ASC")

Dybest set konverterer du bare din forudskrevne forespørgsel til Rails 3-findermetoder.




  1. fatal fejl opstået under udførelse... under opdatering

  2. Opdatering af en tabel i Oracle, hvis en feltværdi er nul, og afgør, at opdateringen er vellykket

  3. ORA-01591 - oracle slet data fra ekstern sql server tabel

  4. password_verify php matcher ikke