使用没有ActiveRecord的Ruby on Rails

时间:2021-12-03 21:43:48

I've been considering switching from using PHP to Ruby on Rails for my web projects and the one thing I'm most concerned about is the use of the ActiveRecord for Rails. It seems to me like using the ActiveRecord is very strongly encouraged and writing straight SQL seems frowned upon. After looking at several projects I have yet to see one that actually uses SQL in it.

我一直在考虑从使用PHP切换到Ruby on Rails,这是我的web项目,我最关心的一件事是使用ActiveRecord的Rails。在我看来,使用ActiveRecord是非常受鼓励的,编写直接的SQL似乎是不受欢迎的。在查看了几个项目之后,我还没有看到一个项目在其中实际使用SQL。

I'm not sold on the ActiveRecord yet, mostly because it requires learning something that doesn't seem as powerful as straight SQL, e.g. is there an easy way to do a nested query with a group by and having clause with the ActiveRecord or will I have to jump through hoops to do so?

我不出售ActiveRecord,主要是因为它需要学习的东西似乎不那么强大直SQL,如有一个简单的方法来做一个嵌套查询与group by和条款ActiveRecord还是我要百依百顺?

Has anyone ever used Rails without the ActiveRecord? If so, what was your experience doing so?

有人在没有ActiveRecord的情况下使用过Rails吗?如果是的话,你的经验是什么?

4 个解决方案

#1


8  

If you don't want to use ActiveRecord you don't have to and can define pure sql in your models if you so wish. find_by_sql('select whatever')

如果您不想使用ActiveRecord,那么您不必在模型中定义纯sql。find_by_sql(选择)

#2


44  

Three words: Here be dragons.

三个字:这是龙。

By going off the path of doing things "The Rails Way", then you will encounter foes of unimaginable power and cunning. At first, you will think "OH HO HO I am doing it my way, aren't I awesome?". Then you'll grow tired. Exhausted even. Forlornly, during your struggles, you'll look at all the cool gems that are using Active Record and wonder why you chose to venture down a path so fraught with doom and gloom it would make Chuck Norris shit his pants.

如果你偏离了“走正道”的路线,那么你将会遇到难以想象的力量和狡猾的敌人。一开始,你会想“哦,我按自己的方式做,不是很棒吗?”然后你会变得很累。甚至筋疲力尽。可悲的是,在你苦苦挣扎的过程中,你会看到所有那些使用活跃记录的酷酷的宝石,你会奇怪为什么你选择冒险走一条充满厄运和阴郁的道路,这会让查克·诺里斯(Chuck Norris)感到恶心。

Please, do not stray from Active Record. It is only here to help you, not hinder you. If you wish to write your own queries then there is a find_by_sql method, or an even lower Model.connection.execute method. However, these should only be used, much like nuclear weaponry, in times of exceptionally dire needs.

请不要偏离活动记录。它只是在这里帮助你,而不是阻碍你。如果您希望编写自己的查询,那么有一个find_by_sql方法,或者更低的Model.connection。执行方法。然而,这些武器只应在特别需要的时候使用,就像核武器一样。

If you don't want to use Active Record at all, then I would encourage you to look at either DataMapper or Mongoid.

如果您不想使用Active Record,那么我建议您查看DataMapper或Mongoid。

DataMapper provides much of the same functionality as Active Record and some have been known to prefer the syntax. As a bit of trivia: it was one of the first gems to be compatible with Rails 3.

DataMapper提供了与活动记录相同的功能,有些已经知道更喜欢这种语法。作为一个小细节:它是第一个与Rails 3兼容的gem之一。

Mongoid on the other hand is for MongoDB databases, which some other people also fancy.

另一方面,Mongoid是用于MongoDB数据库的,其他一些人也喜欢它。

I beg again: do not stray from the beaten path, lest you want a beating yourself.

我再一次请求:不要偏离常轨,以免自己被打。

#3


4  

You could use DataMapper. After writing Rails apps and tools for over a year I would -strongly- recommend using ActiveRecord. Even if you only ever use Model.find_by_sql("error prone hand written sql using fields that may change over time") instead of Model.find_by_last_name("Smith")

您可以使用DataMapper。在编写Rails应用程序和工具一年后,我强烈建议使用ActiveRecord。即使你只使用模型。find_by_sql(“使用可能随时间变化的字段编写的容易出错的sql”)而不是Model.find_by_last_name(“Smith”)

Many smart people have spent many hours working on ActiveRecord and ARL. I beg that you leverage their work.

许多聪明人花了很多时间在ActiveRecord和ARL上。我请求你利用他们的工作。

#4


3  

There must be a good reason for other people to use ORMs in their Rails apps. :)

对于其他人来说,在他们的Rails应用程序中使用orm一定有很好的理由。:)

Especially with Rails 3, ORMs are pluggable - you can easily use DataMapper, or Sequel instead of ActiveRecord.

特别是在Rails 3中,orm是可插拔的——您可以轻松地使用DataMapper,或者Sequel而不是ActiveRecord。

In any case, models are important (and very powerful) part of any MVC framework. Lot of business logic is placed within models - "fat models" is kind of recommended way of development.

在任何情况下,模型都是任何MVC框架的重要(而且非常强大)部分。很多业务逻辑都放在模型中——“胖模型”是一种推荐的开发方式。

Another good point about existing ORMs is that they actually allow (but not encourage) you to write SQL by hand - you could freely write all your queries by hand if you like. But even then, I'd recommend that you leave your queries parametrized and let the library interpolate your parameters (you do that in PHP as well, don't you)?

关于现有orm的另一个好处是,它们实际上允许(但不鼓励)您手工编写SQL——如果您愿意,您可以*地手工编写所有查询。但即便如此,我还是建议您将查询参数化,并让库插入您的参数(您在PHP中也这样做,不是吗?)

#1


8  

If you don't want to use ActiveRecord you don't have to and can define pure sql in your models if you so wish. find_by_sql('select whatever')

如果您不想使用ActiveRecord,那么您不必在模型中定义纯sql。find_by_sql(选择)

#2


44  

Three words: Here be dragons.

三个字:这是龙。

By going off the path of doing things "The Rails Way", then you will encounter foes of unimaginable power and cunning. At first, you will think "OH HO HO I am doing it my way, aren't I awesome?". Then you'll grow tired. Exhausted even. Forlornly, during your struggles, you'll look at all the cool gems that are using Active Record and wonder why you chose to venture down a path so fraught with doom and gloom it would make Chuck Norris shit his pants.

如果你偏离了“走正道”的路线,那么你将会遇到难以想象的力量和狡猾的敌人。一开始,你会想“哦,我按自己的方式做,不是很棒吗?”然后你会变得很累。甚至筋疲力尽。可悲的是,在你苦苦挣扎的过程中,你会看到所有那些使用活跃记录的酷酷的宝石,你会奇怪为什么你选择冒险走一条充满厄运和阴郁的道路,这会让查克·诺里斯(Chuck Norris)感到恶心。

Please, do not stray from Active Record. It is only here to help you, not hinder you. If you wish to write your own queries then there is a find_by_sql method, or an even lower Model.connection.execute method. However, these should only be used, much like nuclear weaponry, in times of exceptionally dire needs.

请不要偏离活动记录。它只是在这里帮助你,而不是阻碍你。如果您希望编写自己的查询,那么有一个find_by_sql方法,或者更低的Model.connection。执行方法。然而,这些武器只应在特别需要的时候使用,就像核武器一样。

If you don't want to use Active Record at all, then I would encourage you to look at either DataMapper or Mongoid.

如果您不想使用Active Record,那么我建议您查看DataMapper或Mongoid。

DataMapper provides much of the same functionality as Active Record and some have been known to prefer the syntax. As a bit of trivia: it was one of the first gems to be compatible with Rails 3.

DataMapper提供了与活动记录相同的功能,有些已经知道更喜欢这种语法。作为一个小细节:它是第一个与Rails 3兼容的gem之一。

Mongoid on the other hand is for MongoDB databases, which some other people also fancy.

另一方面,Mongoid是用于MongoDB数据库的,其他一些人也喜欢它。

I beg again: do not stray from the beaten path, lest you want a beating yourself.

我再一次请求:不要偏离常轨,以免自己被打。

#3


4  

You could use DataMapper. After writing Rails apps and tools for over a year I would -strongly- recommend using ActiveRecord. Even if you only ever use Model.find_by_sql("error prone hand written sql using fields that may change over time") instead of Model.find_by_last_name("Smith")

您可以使用DataMapper。在编写Rails应用程序和工具一年后,我强烈建议使用ActiveRecord。即使你只使用模型。find_by_sql(“使用可能随时间变化的字段编写的容易出错的sql”)而不是Model.find_by_last_name(“Smith”)

Many smart people have spent many hours working on ActiveRecord and ARL. I beg that you leverage their work.

许多聪明人花了很多时间在ActiveRecord和ARL上。我请求你利用他们的工作。

#4


3  

There must be a good reason for other people to use ORMs in their Rails apps. :)

对于其他人来说,在他们的Rails应用程序中使用orm一定有很好的理由。:)

Especially with Rails 3, ORMs are pluggable - you can easily use DataMapper, or Sequel instead of ActiveRecord.

特别是在Rails 3中,orm是可插拔的——您可以轻松地使用DataMapper,或者Sequel而不是ActiveRecord。

In any case, models are important (and very powerful) part of any MVC framework. Lot of business logic is placed within models - "fat models" is kind of recommended way of development.

在任何情况下,模型都是任何MVC框架的重要(而且非常强大)部分。很多业务逻辑都放在模型中——“胖模型”是一种推荐的开发方式。

Another good point about existing ORMs is that they actually allow (but not encourage) you to write SQL by hand - you could freely write all your queries by hand if you like. But even then, I'd recommend that you leave your queries parametrized and let the library interpolate your parameters (you do that in PHP as well, don't you)?

关于现有orm的另一个好处是,它们实际上允许(但不鼓励)您手工编写SQL——如果您愿意,您可以*地手工编写所有查询。但即便如此,我还是建议您将查询参数化,并让库插入您的参数(您在PHP中也这样做,不是吗?)