Sequel 与 ActiveRecord 结合使用有什么问题吗?
我正在考虑使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
免责声明:我是续集维护者。
使用 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.
就我个人而言,我不会这样做。 首先,仅仅手动管理连接或多或少会很乏味。 如果我觉得 Sequel 是更强大的选择,我会更倾向于推迟 Rails 3.0(或者可能开始针对 Edge Rails 进行开发),如果 Yehuda 和他的同事做得正确的话,切换 ORM 应该相当容易。 至少比现在更像 Merb。
这是 DHH 对这个主题的看法(我并不是说它应该被视为福音真理,但可以说,它是来自马口中的):
(引用在这里找到,原文位于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):
(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 thatexecute
can't deal with?Any examples we could look at?