Rails 中的查询不返回任何记录 [英] Query in Rails not returning any records

查看:30
本文介绍了Rails 中的查询不返回任何记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前问题的扩展(使用选择,分配变量并检索记录).

This is an extension of my previous question (using a select, assign variable and retrieve records).

我通过将正确的值传递给控制器​​解决了这个问题,但我的查询仍然没有返回任何记录,但我的数据库中有匹配的记录.

I resolved the problem with passing the correct value to the controller, but my query still does NOT return any records, yet there ARE matching records in my database.

新编辑的应用控制器...

NEW edited controller for the app...

# GET /bentries/january
def monthlysumm
   @bentries = Bentry.by_month(params[:billDate])    
end

这里是模型的补充...

AND here is the addition to the model...

def self.by_month(mon)
   where("strftime('%m', billDate) + 0 = ?", mon)
end

这会生成一个像这样的 SQL 语句...

This generates a SQL statement like this...

SELECT "bentries".* FROM "bentries" WHERE (strftime('%m", billDate) + 0 = '2')

如果我在 Firefox 中进入 SQLite Manager 并输入(等效的)SQL 语句...

If I go into SQLite Manager in Firefox and type (the equivalent) SQL statement of...

SELECT * FROM bentries WHERE (strftime('%m', billDate) + 0 = 2)

它从bentries"表返回预期的 3 条记录,但应用程序中的代码没有生成结果记录集.

It returns the expected 3 records from the "bentries" table, but yet the code in app generates NO resulting recordset.

谁能看到我在控制器或模型中哪里出错了?!?

Can anyone see where I have gone wrong in the controller or model?!?

推荐答案

您的查询不一样.

生成的正在寻找一个字符串,您正在输入的正在寻找一个整数.

The one generated is looking for a string, the one you are typing in is looking for an integer.

参数作为字符串进入控制器,因此您需要在必要时将其设为整数.例如:

Parameters come into the controller as strings so you need to make it an integer if necessary. For example:

def self.by_month(mon)
   where("strftime('%m', billDate) + 0 = ?", mon.to_i)
end

或者您可以在调用 by_month 时执行此操作,具体取决于您希望 by_month 方法中的月份是整数还是字符串.

Or you can do it in the call to by_month, depending on whether you want the month in your by_month method to be an integer or a string.

这篇关于Rails 中的查询不返回任何记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆