我想看一个给定的ActiveRecord Query将生成的SQL语句.我发现在查询发出后我可以从日志中获取此信息,但我想知道是否有可以调用的方法和ActiveRecord Query.
例如:
SampleModel.find(:all, :select => "DISTINCT(*)", :conditions => ["`date` > #{self.date}"], :limit => 1, :order => '`date`', :group => "`date`")
Run Code Online (Sandbox Code Playgroud)
我想打开irb控制台并在最后添加一个方法,该方法将显示此查询将生成的SQL,但不一定执行查询.
鉴于以下AR模型,我希望在给定任务句柄时按姓氏按字母顺序对用户进行排序:
#user
has_many :assignments
has_many :tasks, :through => :assignments
#assignment
belongs_to :task
belongs_to :user
#task
has_many :assignments
has_many :users, :through => :assignments
Run Code Online (Sandbox Code Playgroud)
我想获得一个任务然后导航到其分配的用户,并按字母顺序对用户列表进行排序.
我一直在想我应该能够添加这样的:order
子句has_many :users, :through => :assignments
:
#task.rb
has_many :assignments
has_many :users, :through => :assignments, :order => 'last_name, first_name'
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
如何last_name
在给定任务时对用户进行排序?
我想要一个JavaScript正则表达式,它将使用24小时制来匹配时间,其中时间是否带冒号.
例如,我想以下列格式匹配时间:
0800
23:45
2345
但这不符合无效的时间,如
34:68
5672
我正在将MS Access应用程序(将表链接到MSSQL服务器)迁移到MySQL.
作为克服一些MSAccess表命名问题的一种方法,我正在寻找一种解决方案来添加MySQL表别名,该别名将指向MySQL数据库中的现有表.理想情况下,我想在mysql中创建别名'dbo_customers',它也指向mysql中的customers表.
需要明确的是,我不希望别名这样的查询中的表名:
SELECT * FROM customers AS dbo_customers
Run Code Online (Sandbox Code Playgroud)
但我希望能够发出以下查询:
SELECT * FROM dbo_customers
Run Code Online (Sandbox Code Playgroud)
并让它从customers表返回数据.
仅使用SQL(MySQL)我想选择父子关系的最后一个子行,其中子行按时间戳排序.
例如使用表invoices
和invoice_items
,我想最新的(即最近的时间戳),invoice_items
每个记录invoice
分别.
--------------------------
|Invoices |
--------------------------
|invoice_id| other fields|
--------------------------
| 1 | ... |
--------------------------
| 2 | ... |
--------------------------
| 3 | ... |
--------------------------
--------------------------------------------
|Invoice_Items |
--------------------------------------------
| id | invoice_id | invoice_item_timestamp |
--------------------------------------------
| 1 | 1 | 2009-12-01 10:00:00 |
--------------------------------------------
| 2 | 1 | 2009-12-01 10:01:00 |
--------------------------------------------
| 3 | 1 | 2009-12-01 10:02:00 |
--------------------------------------------
| 4 | 2 | …
Run Code Online (Sandbox Code Playgroud) 我在Ubuntu 10.04上运行的Apache 2中启用了XSendFile模块.我添加了XSendFile on
指令并成功重新启动了Apache服务器.当我添加XSendFilePath /path/to/dir
指令并重新启动Apache时,我收到以下错误:
无效的命令'XSendFilePath',可能由服务器配置中未包含的模块拼写错误或定义
不确定我做错了什么?
使用MYSQL我想重构以下SELECT
语句以返回包含最新内容invoice_date
的整个记录:
> SELECT id, invoice, invoice_date
FROM invoice_items
WHERE lot = 1047
id invoice_id invoice_date
-----------------------------------
3235 1047 2009-12-15 11:40:00
3295 1047 2009-12-15 16:00:00
3311 1047 2009-12-15 09:30:00
3340 1047 2009-12-15 13:50:00
Run Code Online (Sandbox Code Playgroud)
使用MAX()聚合函数和GROUP BY子句让我成为那里的一部分:
> SELECT id, invoice_id, max(invoice_date)
FROM invoice_items
WHERE invoice_id = 1047
GROUP BY invoice_id
id invoice_id invoice_date
-----------------------------------
3235 1047 2009-12-15 16:00:00
Run Code Online (Sandbox Code Playgroud)
请注意,查询似乎MAX(invoice_date)
正确,但id
返回的(3235)不是id
包含MAX(invoice_date)
(3295)的记录,它是id
初始查询中第一条记录的记录.
如何重构此查询以向我提供包含?的整个记录MAX(invoice_date) …
如何在rails3应用程序中着色并可能加粗Rails.logger的输出?
我正在尝试使用乘客2.2.15将Rails 3.0.0应用程序部署到子URI.
我相信我已经RailsBaseURI
对我的http.conf 进行了正确的更改,将子URI符号链接到我的应用程序的公共目录,并将以下代码行添加到environments/production.rb
:
config.action_controller.relative_url_root = "/sub_uri"
Run Code Online (Sandbox Code Playgroud)
我在rails3.0.0之前做了好几次.也就是说,该应用程序将无法启动.它失败并出现以下乘客错误:
Error Message: wrong number of arguments(1 for 0)
Exception class: ArgumentError
/usr/lib/ruby/gems/1.8/gems/actionpack-3.0.0/lib/action_controller/railtie.rb 54 in `relative_url_root='
Run Code Online (Sandbox Code Playgroud)
乘客2.2.15和rails 3.0.0之间是否存在影响子URI的不兼容性?
非常感谢任何帮助整理此错误.
使用ruby正则表达式,如何匹配逗号分隔列表中的所有单词,但只有在整个单词包含有效单词字符(即:字母数字或下划线)时才匹配.例如,给定字符串:
"看,简,跑,r#un,j @ ne,r!n"
我想要配对
'看','简'和'跑',
但不是这些话
'r#un','j @ ne'或'r1n'.
我不想与昏迷相匹配......只是单词本身.
我在这里开始使用正则表达式:http://rubular.com/regexes/12126
sql ×4
mysql ×3
regex ×2
activerecord ×1
apache2 ×1
console ×1
irb ×1
javascript ×1
ms-access ×1
passenger ×1
ruby ×1
sql-server ×1
time ×1
ubuntu-10.04 ×1