在没有 ActiveRecord 的情况下使用 Ruby on Rails

发布于 2024-10-07 22:00:05 字数 372 浏览 0 评论 0原文

我一直在考虑将我的 Web 项目从 PHP 切换到 Ruby on Rails,而我最关心的一件事是 ActiveRecord for Rails 的使用。在我看来,使用 ActiveRecord 是受到强烈鼓励的,而直接编写 SQL 似乎是不受欢迎的。在查看了几个项目之后,我还没有看到一个真正使用 SQL 的项目。

我还没有对 ActiveRecord 感兴趣,主要是因为它需要学习一些看起来不如直接 SQL 强大的东西,例如,是否有一种简单的方法可以使用 ActiveRecord 或 will 进行带有 group by 和having 子句的嵌套查询我必须克服重重困难才能做到这一点吗?

有人曾经使用过没有 ActiveRecord 的 Rails 吗?如果是这样,您这样做的经历是什么?

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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你丑哭了我 2024-10-14 22:00:05

三个词:这里有龙

如果偏离“轨道方式”做事的道路,那么你将遇到难以想象的力量和狡猾的敌人。一开始,您会想“OH HO HO,我正在按照我的方式做事,我不是很棒吗?”。然后你就会变得疲倦。甚至筋疲力尽。不幸的是,在你挣扎的过程中,你会看到所有使用 Active Record 的很酷的宝石并感到惊奇为什么你选择冒险走上一条充满厄运和阴郁的道路,这会让查克·诺里斯尿裤子。

请不要偏离 Active Record。它只是来帮助你,而不是阻碍你。如果您希望编写自己的查询,则可以使用 find_by_sql 方法,或者甚至更低的 Model.connection.execute 方法。然而,这些武器就像核武器一样,只能在极其需要的时候使用。

如果您根本不想使用 Active Record,那么我建议您查看 DataMapperMongoid

DataMapper 提供了许多与 Active Record 相同的功能,并且有些人更喜欢这种语法。顺便说一句:它是最早与 Rails 3 兼容的 gem 之一。

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

我再次恳求:不要偏离常规,以免自己挨打。

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.

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.

If you don't want to use Active Record at all, then I would encourage you to look at either DataMapper or 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.

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

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

撩人痒 2024-10-14 22:00:05

如果您不想使用 ActiveRecord,则不必使用,并且可以在模型中定义纯 sql(如果您愿意)。 find_by_sql('选择任意内容')

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')

秋风の叶未落 2024-10-14 22:00:05

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

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

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")

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

べ映画 2024-10-14 22:00:05

其他人在他们的 Rails 应用程序中使用 ORM 一定有充分的理由。 :)

特别是在 Rails 3 中,ORM 是可插入的 - 您可以轻松使用 DataMapper 或 Sequel 来代替 ActiveRecord。

无论如何,模型是任何 MVC 框架的重要(而且非常强大)的一部分。许多业务逻辑都放置在模型中 - “胖模型”是一种推荐的开发方式。

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

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

Especially with Rails 3, ORMs are pluggable - you can easily use DataMapper, or Sequel instead of 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.

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)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文