使用railswhere插件

地址: http://code.google.com/p/railswhere/
在dos窗口项目目录下:ruby script/plugin install http://railswhere.googlecode.com/svn/tags/railswhere

实例:
   @properties = Property.find(:all, :conditions => Where{|w|
      w.and "apply_number like ?", "%#{params[:apply_number]}%" if params['c_apply_number']
      w.and "creator like ?", "%#{params[:creator]}%" if params['c_creator']
      w.and "p_name like ?", "%#{params[:p_name]}%" if params['c_p_name']
      w.and "agent_person like ?", "%#{params[:agent_person]}%" if params['c_agent_person']
      w.and "apply_date between datetime(?) and  datetime(?)", 
                  "#{date_from_param(params[:fromDate])}",
                  "#{date_from_param(params[:toDate])}" if params['c_range']
      })

注意:

这个插件下载后可以在Rails2.1.2环境中直接运行,如果是2.3.4(我只测试过这两个版本),则需要进行如下的小小改动


# file path RAILS_ROOT/vendor/railswhere/lib/where.rb 大概是169行

# 2.1.2
@criteria = ActiveRecord::Base.send(:sanitize_sql, criteria)

# 2.3.4
@criteria = ActiveRecord::Base.send(:sanitize_sql, criteria, nil)

你可能感兴趣的:(SVN,C#,Ruby,ActiveRecord,Rails)