- 浏览: 245123 次
文章分类
最新评论
-
bluky999:
中间的兼职例子很逗 哈哈哈
tornado: web.py 之 Application -
flingfox63:
学习了,详细,赞个
Ruby变量作用域的类目录结构 -
zhou6711411:
不知是版本问题还是怎么的
class A
...
Ruby变量作用域的类目录结构 -
t284299773:
你在方法中定义方法就相当于在方法中调用lambda!
Ruby变量作用域的类目录结构(补二) -
lnj888:
很是有用 不错Powerpoint converter
一个简单的link_to,ROR到底在背后做了些什么?(未完)
原文出处:http://railsontherun.com/2007/9/16/ambition-why-should-you-care
By now, you should have heard about ambition if not read the latest post from the author.
Ambition has a simple goal: making you stop writing SQL in your queries and only stick to Ruby. (who cares if you use ActiveRecord, Sequel, DataMapper or another ORM)
I'm so used to the ActiveRecord way of querying the database that I was not fully convinced that Ambition would help me in my daily tasks. I still gave it a try:
Testing Ambition
$ sudo gem install ambition -y
Started my console
$ script/console
and required Ambition
require 'ambition'
I started by doing a query the AR way:
1 2 |
Photo.find(:all, :conditions => "photos.title IS NULL AND photos.width > 250 AND photos.height > 200 AND users.name = 'test'", :include => :user) |
And I converted it into an Ambition call:
|
Photo.select {|p| p.title == nil && p.width > 250 && p.height > 200 && p.user.name == 'test'}.entries |
145 vs 102 keystrokes. 30% less typing with Ambition! I don't know about you, but I REALLY prefer the Ruby only query, much cleaner and much "DRYer". However, that's not always true:
|
Photo.find_by_title(nil) |
(24chars)
|
Photo.detect{|p| p.title == nil} |
But what's going on behind the scene? Do we have the exact same SQL query sent to our DB?
Well, Ambition doesn't generate any SQL, it uses AR to do so. You want to make sure Ambition is not messing with you, try that:
1 2 |
>> Photo.select {|p| p.title == nil && p.width > 250 && p.height > 200 && p.user.name == 'test'}.to_hash => {:conditions=>"(photos.`title` IS NULL AND (photos.`width` > 250 AND (photos.`height` > 200 AND users.name = 'test')))", :include=>[:user]} |
That's pretty hot. Especially when you have to use eager loading!
Obviously you can still do stuff like that:
1 2 3 |
Photo.select {|p| p.title == nil && p.width > 250 && p.height > 200 && p.user.name == 'test'}.each do |photo| puts photo.filename end |
(note the query will only be made once)
Another cool thing, is to do simple sorting:
|
>> Photo.select {|p| p.title == nil && p.user.name == 'test'}.sort_by { |p| [p.created_at, -p.size] } |
creates the following:
|
=> {:order=>"photos.created_at, photos.size DESC", :conditions=>"(photos.`title` IS NULL AND users.name = 'test')", :include=>[:user]} |
or
|
=> "SELECT * FROM photos JOIN user WHERE (photos.`title` IS NULL AND users.name = 'test') ORDER BY photos.created_at, photos.size DESC"
|
That's cool, and you can still sort on relationships:
1 2 3 4 5 6 7 |
>> Photo.select {|p| p.title == nil }.sort_by { |p| p.user.name } => "SELECT * FROM photos JOIN user WHERE photos.`title` IS NULL ORDER BY users.name"</macro:code > Or directly on the model: <macro:code lang="ruby">>> Photo.sort_by(&:title) => "SELECT * FROM photos ORDER BY photos.title" |
To finish, another detail which makes Ambition a great library
1 2 3 |
>> Photo.any? {|p| p.title =~ /ambition/ } => "SELECT count(*) AS count_all FROM photos WHERE (photos.`title` REGEXP 'ambition')" => true |
And if you were worried that it wouldn't work with utf8, check this out:
1 2 3 4 5 6 |
>> Photo.any? {|p| p.title == 'école'} => SET NAMES 'utf8' => SET SQL_AUTO_IS_NULL=0 => SHOW FIELDS FROM photos => SELECT count(*) AS count_all FROM photos WHERE (photos.`title` = 'école') => false |
Limitations
The only limitation I found in Ambition is that Ruby code won't work in the block, for instance:
|
>> Photo.select {|p| p.title == nil && p.created_at < 1.week.ago && p.user.name == 'test'}.entries |
won't work at the moment. To inspect what's going simply try:
1 2 |
>> Photo.select {|p| p.title == nil && p.created_at < 1.week.ago && p.user.name == 'test'}.to_sql => "SELECT * FROM photos JOIN user WHERE (photos.`title` IS NULL AND (photos.`created_at` < 1.`week`.`ago` AND users.name = 'test'))" |
You can see that photos.created_at
< 1.week
.ago
is the problem.
The recommended way to achieve the same result is to use variables:
1 2 3 |
>> date = 1.week.ago >> Photo.select {|p| p.title == nil && p.created_at < date && p.user.name == 'test'}.to_sql => "SELECT * FROM photos JOIN user WHERE (photos.`title` IS NULL AND (photos.`created_at` < '2007-09-08 19:38:48' AND users.name = 'test'))" |
However, note that method calls will work just fine:
1 2 3 4 5 6 |
>> def time_now_please >> Time.now >> end >> Photo.select {|p| p.title == nil && p.created_at < time_now_please && p.user.name == 'test'}.to_sql => "SELECT * FROM photos JOIN user WHERE (photos.`title` IS NULL AND (photos.`created_at` < '2007-09-15 19:41:37' AND users.name = 'test'))" |
Conclusion
For now, Ambition is still just wrapping ActiveRecord::Base#find but the plan is to actually generate SQL. Hopefully we'll also be able to use Ruby code from within an Ambition block. Kickers methods are very interesting and could become a really nice way of speeding up your app and keep your code clean.
Ambition is a great query library, I think I'll start using it whenever I have "find" calls with multiple conditions especially if my conditions are related to another model. However I still didn't figure out how to use an inner join with Ambition.
发表评论
-
(ZZ)Ror on svn
2007-12-20 19:34 1516正好需要,zz过来,抄袭自:http://www.surui. ... -
用GetText来进行ROR的国际化和本地化
2007-11-22 15:17 1447IBM developerWorks上的一篇文章,直接贴地址, ... -
advanced act_as_solr
2007-10-31 19:40 1797原文出处:http://www.quarkruby.com/2 ... -
act_as_solr
2007-10-31 19:39 1998原文出处:http://www.quarkruby.com/2 ... -
使用Inkscape提供自己的pdf服务
2007-10-31 19:34 1558原文出处:http://www.thesatya.com/bl ... -
给will_paginate加上ajax效果
2007-10-31 19:30 2160原文出处:http://railsontherun.com/2 ... -
使用rails制作图表
2007-10-31 19:21 2821原文出处:http://www.railsontherun.c ... -
如果定制attachment_fu上传文件的路径和文件名
2007-10-31 16:59 2756原文出处:http://the.railsi.st/2007/ ... -
attachment_fu使用指南
2007-10-31 16:56 3216原文出处:http://clarkware.com/cgi/b ... -
(ZZ)Cache in Rails
2007-09-25 15:49 1521很经典的文章,留在blog里面做个收藏 Ruby on Rai ... -
(ZZ)Ruby on Rails Security Guide
2007-09-24 21:28 2689Ruby on Rails Security Gui ... -
学到三招
2007-09-24 01:54 1399第一招:用ruby-debug来调试rails程序 具体使用方 ... -
一个action的process过程
2007-09-17 00:11 2555ruby 代码 def process(req ... -
在线查看rails代码和edge rails api的网址,备份,以免忘记
2007-09-14 18:38 1339Edge Rails API: http://caboo.se ... -
总是看到returning,这到底是个什么东东,查了一下找到了源代码
2007-09-14 18:37 1394A Ruby-ized realization of the ... -
一个简单的link_to,ROR到底在背后做了些什么?(未完)
2007-09-14 18:20 3474滥用link_to会造成ror程序 ... -
学到关于include的一点儿知识
2007-08-23 18:09 1173ruby 代码 module Test ... -
在一个controller中render另外一个controller中view的时候出现问题
2007-08-21 18:27 2161我想在posts这个controller中的show.rh ... -
因为Rjs试用NetBeans
2007-06-20 09:44 1133因为昨天看Rails Recipe的时候提到了rjs,于是四处 ...
相关推荐
【标题】"AMBITION安邦信G7 P7系列变频器说明书.rar" 提供的是一份关于AMBIION安邦信公司生产的G7和P7系列变频器的详细操作指南。这份文档旨在帮助用户理解和操作这两个系列的变频器,确保其在工业应用中的高效、...
AMBITION-QMS质量管理信息系统介绍V21汇编.pdf
【标题】"AMBITION安邦信AMB-V11系列变频器说明书.rar"是一个压缩文件,其中包含了关于AMB-V11系列变频器的详细使用和操作指南。这个系列的变频器是安邦信公司产品线的一部分,专门设计用于控制电机的速度和扭矩。 ...
标题中的“AMBITION安邦信G11系列变频器说明书.rar”表明这是一份关于AMBITION品牌下安邦信G11系列变频器的详细操作和使用指南,通常包含产品的技术参数、安装方法、操作步骤、故障排查等内容。变频器是工业自动化...
标题中的"PyPI 官网下载 | ambition_edc-0.3.46-py3-none-any.whl"指的是在Python的包索引服务(Python Package Index,简称PyPI)上发布的ambition_edc库的一个特定版本。PyPI是Python开发者分享和发现软件包的主要...
《Python库:ambition-prn-0.1.8.macosx-10.13-x86_64.tar.gz深度解析》 在IT领域,Python作为一种强大的开发语言,因其简洁明了的语法和丰富的库支持而备受青睐。本文将深入探讨一个名为"ambition-prn"的Python库...
标题"AMBITION安邦信AMB-E11系列变频器说明书.rar"指出这是一份关于AMB-E11系列变频器的详细说明书,文档以RAR压缩格式提供,通常包含多页或者多个文件,便于用户下载和存储。"安邦信"是一家知名的变频器生产商,而...
《AMBITION安邦信AMB-HVI高压系列变频器说明书》是指导用户全面掌握这款设备的重要文献,涵盖了从理论知识到实际操作的全方位指导。 首先,AMB-HVI高压系列变频器是通过改变供电电压的频率来调节电机转速的原理工作...
Company_name:Ambition Box 上列出的 公司名称。 描述:对公司的简要描述,通常来自其简介或网站。 评分:用户在 Amb ition Box 上提供的公司的总体评分。 Highly_rated_for: 用户高度评价的公司领域或方面。 ...
《PyPI官网下载:ambition-prn-0.1.29.macosx-10.7-x86_64.tar.gz详解》 在Python的开发和分发环境中,PyPI(Python Package Index)扮演着至关重要的角色。它是一个庞大的仓库,存储了无数的Python软件包,供...
标题中的"ambition-ae-0.3.2.macosx-10.13-x86_64.tar.gz"表明这是一个针对Python的库,名为"ambition-ae",版本号为0.3.2,专为macOS 10.13操作系统(High Sierra)设计的64位架构。这种文件格式是tar归档文件,...
标题中的"PyPI 官网下载 | ambition_rando-0.1.40-py3-none-any.whl"指的是在Python的包索引服务(Python Package Index,简称PyPI)上发布的ambition_rando库的一个特定版本,0.1.40。这个库是为Python 3编写的,...
东南大学电类专业保研外校指导手册_ambition-cookbook
Americans ambition 英语课前演讲PPT课件PPT学习教案.pptx
【标题】:Ambition Clothing Co. 乔治亚州亚特兰大服装品牌网站设计与CSS技术解析 在当今数字化的世界中,网站是品牌形象的重要展示窗口。"Ambition Clothing Co.",这家位于美国乔治亚州亚特兰大的服装品牌,通过...
资源来自pypi官网。 资源全名:ambition_visit_schedule-0.1.18-py3-none-any.whl
资源分类:Python库 所属语言:Python 资源全名:ambition_rando-0.1.18-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059