vendredi 26 juin 2015

Rails. Why should i use ActiveRecord?

I was trying to make simple SQL-Query tasks. So i used active record and SQLite for my development environment with PostgreSQL for my prod. env.

I thought ActiveRecord is used because it can generate queries depending on DB used but all my queries have some errors for PostgreSQL.

Queries:

@sql[0] = Task.select(:done, :deadline).order(name: :asc).distinct

@sql[1] = Task.joins(:project).group(:project_id).select("projects.name, 
COUNT(*) as TaskCount").order("TaskCount DESC")

@sql[2] = Task.joins(:project).group(:project_id).select("projects.name, 
COUNT(*) as TaskCount").order("projects.name ASC")

@sql[3] = Task.select("projects.name AS pName","tasks.*")
.joins(:project).where("projects.name LIKE ?",'N%')
          .where("projects.name LIKE ?","%_a_%")'

@sql[4] = Project.joins("LEFT OUTER JOIN tasks 
ON 'projects'.'id'='tasks'.'project_id'")
.group(:project_id)
.select("projects.*, COUNT(tasks.project_id) as TaskCount")
          .where("projects.name LIKE ?","%_a_%")

@sql[5] = Task.group(:name).having("COUNT(*)>1").order(name: :asc)

@sql[6] = Task.joins(:project).where("projects.name = 'Garage'")
.group("tasks.name, tasks.done, tasks.deadline")
.having("COUNT(*)>1").select("tasks.*, COUNT(*)").order("COUNT(*) DESC")

@sql[7] = Task.where("tasks.done = ?",true).joins(:project).group(:project_id)
.having("COUNT(*)>=10").select("projects.name, COUNT(*) as TaskCount")
.order("projects.id DESC")

Every of them has some errors.

I dont expect you to solve them.

  • My question how would i avoid them in a first place?
  • Should i use PostgreSQL for development as well?
  • What the purpose of active record then? Can i just write pure queries? Because it seems like a better choice. (Maybe i am wrong?)



Aucun commentaire:

Enregistrer un commentaire