简体   繁体   English

轨。我为什么要使用ActiveRecord?

[英]Rails. Why should i use ActiveRecord?

I was trying to make simple SQL-Query tasks. 我试图做简单的SQL查询任务。 So i used active record and SQLite for my development environment with PostgreSQL for my prod. 所以我使用活动记录和SQLite为我的开发环境使用PostgreSQL作为我的产品。 env. ENV。

I thought ActiveRecord is used because it can generate queries depending on DB used but all my queries have some errors for PostgreSQL. 我认为使用ActiveRecord是因为它可以根据所使用的数据库生成查询,但我的所有查询都有一些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? 我应该使用PostgreSQL进行开发吗?
  • What the purpose of active record then? 那么积极记录的目的是什么呢? Can i just write pure queries? 我可以写纯粹的查询吗? Because it seems like a better choice. 因为它似乎是一个更好的选择。 (Maybe i am wrong?) (也许我错了?)

This probably deserves an appropriate answer. 这可能值得一个合适的答案。

My question how would i avoid them in a first place? 我的问题我如何首先避开它们?

First things first - keep a copy of the rails guides for postgres handy. 首先要做的事情 - 保留postgres导轨指南的副本。 That will cover most bases. 这将涵盖大多数基地。 You usually will encounter them in the wild - examples like these abound. 你通常会在野外遇到它们 - 像这样的例子比比皆是。 SO does a very good job at pointing you in the right direction. 所以在指出正确方向方面做得非常好。

Should i use PostgreSQL for development as well? 我应该使用PostgreSQL进行开发吗?

Absolutely. 绝对。 Heroku nails it on the head : Heroku 把它钉在头上

Any divergence between the development of an application and its execution in production can cause tiny incompatibilities, causing code that worked in development or staging to fail in production. 应用程序的开发与其在生产中的执行之间的任何差异都可能导致微小的不兼容性,从而导致在开发或暂存中工作的代码在生产中失败。 For instance, avoid using different services between development and production, even when adapters theoretically abstract away any differences in services. 例如,避免在开发和生产之间使用不同的服务,即使适配器理论上抽象出服务中的任何差异。 Using SQLite locally and PostgreSQL in production; 在本地使用SQLite和在生产中使用PostgreSQL; or local process memory for caching in development and Memcached in production, may seem innocuous at first but can be the cause of a variety of production issues. 或者用于开发中的缓存和生产中的Memcached的本地进程内存,起初可能看起来无害,但可能是各种生产问题的原因。

More reading from this SO post 更多阅读这篇SO帖子

Oh, and: 哦,并且:

Q: What the purpose of active record then? 问:活动记录的目的是什么呢? Can i just write pure queries? 我可以写纯粹的查询吗? Because it seems like a better choice. 因为它似乎是一个更好的选择。 (Maybe i am wrong?) (也许我错了?)

People fall on both sides of this contention. 人们对这一论点的双方都存在争议。 Some people say we just give up ORM altogether because it is an antipattern . 有人说我们完全放弃ORM,因为它是反模式 You will find staunch supporters on both sides of the issue. 你会在这个问题的两边找到坚定的支持者。

What we do know, is that it is a useful tool that should be used with a decent amount of underlying knowledge about the SQL getting spit out. 我们所知道的是,它是一个有用的工具,应该与关于SQL吐出的大量潜在知识一起使用。 It certainly is handy, but be prepared to roll your sleeves up if you find you are executing unperformant queries or something is not working as expected - you can always write pure SQL when needed. 它当然很方便,但如果您发现执行不正确的查询或者某些事情没有按预期工作,请准备好卷起袖子 - 您总是可以在需要时编写纯SQL Cheers. 干杯。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM