Sequel 与 ActiveRecord 结合使用有什么问题吗?

发布于 2024-07-30 10:53:06 字数 220 浏览 7 评论 0原文

我正在考虑使用 Sequel 来处理一些我发现在 Active 中很难制作的复杂 SQL记录。

在同一个项目上使用 Sequel 和 ActiveRecord 时,有什么需要注意的吗? (除了像续集中没有 AR 验证等显而易见的问题......)

I'm considering using Sequel for some of my hairier SQL that I find too hard to craft in Active Record.

Are there any things I need to be aware of when using Sequel and ActiveRecord on the same project? (Besides the obvious ones like no AR validations in sequel etc...)

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

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

发布评论

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

评论(2

迷爱 2024-08-06 10:53:07

免责声明:我是续集维护者。

使用 Rails 时,Sequel 很容易与 ActiveRecord 一起使用或代替 ActiveRecord 使用。 您确实必须手动设置数据库连接,但除此之外,用法是相似的。 您的 Sequel 模型文件位于应用程序/模型中,其工作方式与 ActiveRecord 模型类似。

设置数据库连接并不乏味,通常是在environment.rb中的一行需要sequel,并且在每个环境文件(development.rb,test.rb,Production.rb)中的一行来执行类似的操作:

DB = Sequel .connect(...)

因此,如果您认为 4 行设置代码很乏味,那么它只是乏味的。

除非您的目标是多个数据库,否则使用原始 SQL 通常不会有问题。 避免它的主要原因是增加了冗长。 Sequel 支持使用原始 SQL 至少与 ActiveRecord 一样容易,但在 Sequel 中需要使用原始 SQL 的情况通常相当罕见。

顺便说一句,Sequel 附带了多个验证插件。 validation_class_methods 插件与 ActiveRecord 验证类似,使用类方法。 validation_helpers 插件使用实例级方法有一个更简单的实现,但两者可以做大致相同的事情。

最后,我要说的是,如果您已经拥有可以执行您想要的操作的 ActiveRecord 代码,那么可能不值得将代码移植到 Sequel,除非您计划添加功能。

Disclaimer: I'm the Sequel maintainer.

Sequel is easy to use along side of or instead of ActiveRecord when using Rails. You do have to setup the database connection manually, but other than that, the usage is similar. Your Sequel model files go in app/models and work similarly to ActiveRecord models.

Setting up the database connections isn't tedious, it's generally one line in environment.rb to require sequel, and a line in each environment file (development.rb, test.rb, production.rb) to do something like:

DB = Sequel.connect(...)

So it's only tedious if you consider 4 lines of setup code tedious.

Using raw SQL generally isn't a problem unless you are targeting multiple databases. The main reason to avoid it is the increased verbosity. Sequel supports using raw SQL at least as easily as ActiveRecord, but the times where you need to use raw SQL are generally fairly rare in Sequel.

BTW, Sequel ships with multiple validation plugins. The validation_class_methods plugin is similar to ActiveRecord validations, using class methods. The validation_helpers plugin has a simpler implementation using instance level methods, but both can do roughly the same thing.

Finally, I'll say that if you already have working ActiveRecord code that does what you want, it's probably not worth the effort to port the code to Sequel unless you plan on adding features.

開玄 2024-08-06 10:53:07

就我个人而言,我不会这样做。 首先,仅仅手动管理连接或多或少会很乏味。 如果我觉得 Sequel 是更强大的选择,我会更倾向于推迟 Rails 3.0(或者可能开始针对 Edge Rails 进行开发),如果 Yehuda 和他的同事做得正确的话,切换 ORM 应该相当容易。 至少比现在更像 Merb。

这是 DHH 对这个主题的看法(我并不是说它应该被视为福音真理,但可以说,它是来自马口中的):

但是Sql不是很脏吗?

自从程序员开始
面向对象系统位于顶层
关系数据库,他们已经
苦于如何解决这个问题
深入运行抽象。 一些
对象关系映射器寻求
完全消除 SQL 的使用,
努力实现面向对象的纯粹性
强制所有查询通过另一个 OO
层。

Active Record 没有。 它被建造了
SQL 两者都不是
脏也不坏,只是冗长
琐碎的案件。 重点是
消除了处理的需要
在那些琐碎的情况下冗长但是
保持表现力
硬查询——SQL 的类型
旨在优雅地处理问题。

因此,你不必感到内疚
当你使用 find_by_sql() 来处理
要么是性能瓶颈,要么是硬
查询。 开始使用
面向对象的接口
生产力和快乐,以及下降
在表面之下
接近金属的体验,当你
需要。

(引用在这里找到,原文位于AWDRWR 的 p334,“吊床”书)。

我认为这是合理的。

我们正在讨论 find_by_sql 无法处理的事情吗? 或者我们正在谈论 execute 无法处理的复杂的非 SELECT 内容?

有什么例子我们可以看一下吗?

Personally, I wouldn't do it. Just managing connection more-or-less by hand would be tedious, for a start. I'd be more inclined, if I felt Sequel was the stronger option, to hold off for Rails 3.0 (or perhaps start developing against Edge Rails) where it should be fairly easy to switch ORMs, if Yehuda and co are doing their stuff right. A lot more Merb-like than now, at least.

This was DHH's take on the subject (I'm not saying it should be taken as gospel truth, mind, but it is, so to speak, from the horse's mouth):

But Isn’t Sql Dirty?

Ever since programmers started to
layer object-oriented systems on top
of relational databases, they’ve
struggled with the question of how
deep to run the abstraction. Some
object-relational mappers seek to
eradicate the use of SQL entirely,
striving for object oriented purity by
forcing all queries through another OO
layer.

Active Record does not. It was built
upon the notion that SQL is neither
dirty nor bad, just verbose in the
trivial cases. The focus is on
removing the need to deal with the
verbosity in those trivial cases but
keeping the expressiveness around for
hard queries – the type SQL was
created to deal with elegantly.

Therefore, you shouldn’t feel guilty
when you use find_by_sql() to handle
either performance bottlenecks or hard
queries. Start out using the
object-oriented interface for
productivity and pleasure, and the dip
beneath the surface for a
close-to-the-metal experience when you
need to.

(Quote was found here, original text is on p334 of AWDRWR, the "hammock" book).

I think that's reasonable.

Are we talking about something that find_by_sql can't handle? Or are we talking about complex non-SELECT stuff that execute can't deal with?

Any examples we could look at?

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