【问题标题】:Rails MySql:Empty result in development modeRails MySql:开发模式下的空结果
【发布时间】:2012-03-15 14:36:31
【问题描述】:

我在我的控制器中接到了这个电话:

@tournaments = Tournament.unignored.all(
:include => [:matches,:sport,:category],
:conditions=> ["matches.status in (0,4) 
               && matches.date < ?",
               Time.now.end_of_week + 1.day],
:order => "sports.sort ASC, categories.sort ASC, tournaments.sort ASC")

在生产模式和开发控制台中一切正常。但是当我尝试在开发模式下浏览特定页面时,我得到:

评估nil.each时发生错误

当我将创建的SQL 查询粘贴到MySQL 浏览器中时,会有结果。

It refers to mysql2 (0.2.11) lib/active_record/connection_adapters/mysql2_adapter.rb:587:in `select'

这个查询正确到达。

有没有人遇到过类似的问题?这个错误不知从何而来。没有更新等...

谢谢!

Rails 3.0.9 MySql 5.5 Ruby 1.8.7 and mysql2 0.2.11 gem

【问题讨论】:

  • 包含的类别搞砸了。仍然不知道为什么它可以在服务器上运行。

标签: mysql ruby-on-rails ruby


【解决方案1】:

看来您需要使用:joins 而不是:include

all(以及findwhere 等)的:include 选项告诉rails 单独执行查询以加载给定关联记录的所有必要数据。

:join 选项让 Rails 执行 SQL 查询,将关联模型连接起来,以便您可以查询它们的字段。

如果您想同时查询字段并将它们预加载到关联中,您需要同时执行以下操作:

@tournaments = Tournament.unignored.all(
:include => [:matches,:sport,:category],
:joins => [:matches,:sport,:category],
:conditions=> ["matches.status in (0,4) 
               && matches.date < ?",
               Time.now.end_of_week + 1.day],
:order => "sports.sort ASC, categories.sort ASC, tournaments.sort ASC")

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2012-03-23
    • 2011-07-16
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2010-12-10
    相关资源
    最近更新 更多