如何进行 Active Record 到 Sequel 的转换
我正在使用 Rails3.rc 和 Active Record 3(带有 meta_where),并且刚刚开始切换到 Sequel,因为它似乎更快并且提供了一些非常出色的功能。
我已经在使用 active_model 插件(以及其他一些插件)。
据我所知,我应该使用
User[params[:id]]
而不是User.find(params[:id])
。但是,如果不存在记录,则不会引发此问题,并且不会将值转换为整数(PK 类型),因此它在where
子句中作为字符串。我不确定这是否会导致任何性能问题。这会损害identity_map
吗?解决这两个问题的最佳方法是什么?是否有一种简单的方法可以翻转
User.messages_dataset
和User.messages
等关联的使用,以便User.messages
的行为类似于在 Active Record (User.messages_data_set
) 中。我想我会经常使用#..._dataset
但永远不需要数组方法,因为我可以只添加.all
?我注意到有时一些相同的(复杂的)查询会在一个操作中执行多次。是否有类似 Active Record 查询缓存的东西? (
identity_map
似乎不适用于这些情况)。是否有一个
to_sql
我可以调用来获取数据集将生成的原始SQL?
I'm using Rails3.rc and Active Record 3 (with meta_where) and just started to switch to Sequel, because it seems to be much faster and offers some really great features.
I'm already using the active_model plugin (and some others).
As far as I know, I should use
User[params[:id]]
instead ofUser.find(params[:id])
. But this doesn't raise if no record exists and doesn't convert the value to an integer (type of PK), so it's as a string in thewhere
clause. I'm not sure if this is causing any performance issues. Does this harmidentity_map
? What's the best way to solve both these issues?Is there an easy way to flip the usage of associations like
User.messages_dataset
andUser.messages
so thatUser.messages
behaves like in Active Record (User.messages_data_set
). I guess I'd use the#..._dataset
a lot but never need the array method, because I could just add.all
?I noticed some same (complex) queries are executed several times within one action sometimes. Is there something like the Active Record query cache? (
identity_map
doesn't seem to work for these cases).Is there a
to_sql
I can call to get the raw SQL a dataset would produce?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用:
或者编写您自己的方法来执行类似的操作。 Sequel 支持非整数主键,因此它不会自动进行转换。它不应该对身份映射产生任何影响。请注意,除非您使用 Identity_map 插件,否则 Sequel 不会使用身份映射。我想最好的方法是编写自己的辅助方法。
不是真的。您可以使用association_proxies插件,以便将非数组方法发送到数据集而不是对象数组。一般来说,您不应该过多使用关联数据集方法。如果您经常使用它,则表明您应该对该特定用法有关联。
查询缓存存在且永远不会存在。您应该编写您的操作,以便缓存并重用第一个查询的结果。
Dataset#sql
为您提供数据集的SELECT SQL
。You can either use:
Or write your own method that does something like that. Sequel supports non-integer primary keys, so it wouldn't do the conversion automatically. It shouldn't have any affect on an identity map. Note that Sequel doesn't use an identity map unless you are using the identity_map plugin. I guess the best way is to write your own helper method.
Not really. You can use the
association_proxies
plugin so that non-array methods are sent to the dataset instead of the array of objects. In general, you shouldn't be using the association dataset method much. If you are using it a lot, it's a sign that you should have an association for that specific usage.There is and will never be a query cache. You should write your actions so that the results of the first query are cached and reused.
Dataset#sql
gives you theSELECT SQL
for the dataset.