论坛首页 编程语言技术论坛

插件rquerypad, 简化查询参数 增强关联查询能力

浏览 8413 次
该帖已经被评为良好帖
作者 正文
   发表时间:2008-03-18  
http://code.google.com/p/rquerypad/
目前支持rails 2.0, rails 1.2(部分2.0 方式的查询))(谢谢Quake的测试及建议)

Simplify query options with association automation and improve inner join for activerecord of rails
字段名包含关联信息,不需额外再写include或者joins
原来activerecord仅支持inner join在最后,现在没有这个限制

不想用svn,直接下载可以到 https://rubyforge.org/projects/rquerypad/

Feature
=======
1. single name string decribe associations
2. support to mix inner join and outer join with any order
3. auto remove duplicated joins from other association in different depth
4. support :conditions,rder, :group
5. auto merge to original :include, :joins

Install
=======
ruby script/plugin install http://rquerypad.googlecode.com/svn/trunk/rquerypad

Example
=======
suppose the asscociations of User <-> Thread <-> Reply is 1:N:N

@users = User.find(:all, :group => ["threads.created_at", "name"])
generate:
[:all, {:group=>"threads.created_at, users.name", :include=>[:threads]}]

@users = User.find(:all, :conditions => ["threads_.replies.title = ?",
"rquerypad"])
generate:
[:all, {:inner_joins=>["threads"], :conditions=>["replies.title = ?", "rquerypad"], :include=>[{:threads=>:replies}]}]
#note: the :inner_joints is processed by rquerypad before sending sql to database

@users = User.find(:all, :conditions => ["threads.replies.title = ? and threads.id = ?", "rquerypad", 1])
generate:
[:all, {:conditions=>["replies.title = ? and threads.id = ?", "rquerypad", 1], :include=>[{:threads=>:replies}]}]
#note: single "threads" was removed from includes


Setup
=======
#to set debug model, in rails initialized script
$RQUERYPAD_DEBUG = true

#to support rails 1.2.6, in rails initialized script, such as environment.rb
#default support rails 2.0
$RQUERYPAD_RAILS = "1.2"


Test
=======
Note: current migrate script works only in rails 2.0

1.Prepare

the test depends on sqlite3 database, the following code should be add into your database.yml and place rquerypad.rb(copy from test.rb) in config/environment

rquerypad:
  adapter: sqlite3
  database: vendor/plugins/rquerypad/test/db.rquerypad
  timeout: 5000

2.database migrate

execute the following script

rake migrate

3.start test

execute the following script
rake
   发表时间:2008-03-19  
这东西很棒!这样就可以像hibernate那样写关联查询语句,不需要手动指明join了。
0 请登录后投票
   发表时间:2008-03-20  
强啊
不过怎么添加到自己的项目中中的model和controller中用呢
0 请登录后投票
   发表时间:2008-03-20  
装好plugin,如果是rails 2.0直接就可以用了

如果是rails 1.2 需要在environment里设置一个$RQUERYPAD_RAILS = "1.2" ,而且只支持2.0推荐的方式查询,例如find(:all)而不能用find_all(这个在2.0已经被删除了,谢谢Quake帮助测试)

另外好消息是,今天在https://rubyforge.org/上首页新闻推荐了rquerypad
0 请登录后投票
   发表时间:2008-03-20  
示例当中的:@users = User.find(:all, :conditions => ["threads_.replies.title = ?", 
"rquerypad"]) 
当中的threads_这个下划线是不是多打了,笔误?
0 请登录后投票
   发表时间:2008-03-20  
有下划线的表示该连接使用inner join,后面的注释没有写的很清楚,我会补上去
0 请登录后投票
   发表时间:2008-03-20  
我的是1.2.3版本  好像不行
undefined method `extract_options!' for [:all]:Array
(eval):9:in `find'

这里有一句@goal_status = GoalStatus.find(:all)


不知道是什么原因

在源代码中看到有个这个:
options = #{if $RQUERYPAD_RAILS == "1.2" then "extract_options_from_args!(args)" else "args.extract_options!" end}


可是有已经写上$RQUERYPAD_RAILS == "1.2"了啊

$RQUERYPAD_DEBUG = true 要写吗?写在什么地方?
0 请登录后投票
   发表时间:2008-03-20  
不好意思,要在environments.rb 的顶行写,保证在插件初始化之前,我还要补点注释

而且对rails 1.2兼容不是全兼容,find_all就不能兼容,只能兼容find(:all)

rquerypad 最新的是0.1.4版本,加了计算函数的支持,如 count/average
0 请登录后投票
   发表时间:2008-03-21  
在Model使用 acts_as_tree 时发现有点问题
比如:
class Product < ActiveRecord::Base
acts_as_tree
end

products = Product.find(:all)

products.each do |p|
  p.children.each .....   这里就报错
  ..............
end

插件一安装就出这个问题,不知道是使用不当还是? 

环境 Rails 2.0.2
0 请登录后投票
   发表时间:2008-03-21  
看看你报的是什么错?我并刚刚试用了act_as_tree,修复了一个bug,但跟act_as_tree关系不大,我试过find all, tree object.each 没问题

fix bug when field is a id with single table which has belongs_to association, such as "parent.id"

现在最新版是提交了0.1.5,并增加了相关act_as_tree的测试,你可以install最新的,如果还有问题,你贴一下测试代码,测试很简单,装了sqlite3(注意还要装sqlite3.dll到ruby/bin),然后再rquerypad下rake migrate, rake
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics