【问题标题】:Rails where clause is not working for some of the valuesRails where 子句不适用于某些值
【发布时间】:2016-03-15 07:21:06
【问题描述】:

我正在开发 Easy Redmine PMS,并且我已经在 rails 中编写了 where 查询,例如:

Tracker.where(name: "+ Permits Required")

但它返回的值较少,而当我尝试检测查询以获取相同的数据时,例如:

Tracker.all.detect{|i| i.name == "+ Permits Required"

它正在返回所有值。我还注意到,当直接在数据库中获取数据时,我得到的值为Request Available,而rails 控制台返回Permits Required。我已经用 postgresql 和 mysql 这两个数据库尝试过这个问题,问题是一样的。以下是查询:

2.1.1 :001 > @tra = Tracker.find_by_name("+ Camps & Finished Food Store")
  Tracker Load (32.8ms)  SELECT  "trackers".* FROM "trackers" WHERE "trackers"."name" = $1 LIMIT 1  [["name", "+ Camps & Finished Food Store"]]
 => nil 
2.1.1 :002 > @tra = Tracker.all.detect{|i| i.name == ("+ Camps & Finished Food Store")}
  Tracker Load (0.9ms)  SELECT "trackers".* FROM "trackers"
  AnonymousUser Load (27.4ms)  SELECT  "users".* FROM "users" WHERE "users"."type" IN ('AnonymousUser')  ORDER BY "users"."id" ASC LIMIT 1
  EasyTranslation Load (28.6ms)  SELECT  "easy_translations".* FROM "easy_translations" WHERE "easy_translations"."entity_id" = $1 AND "easy_translations"."entity_type" = $2 AND "easy_translations"."entity_column" = $3 AND "easy_translations"."lang" = $4  ORDER BY "easy_translations"."id" ASC LIMIT 1  [["entity_id", 3], ["entity_type", "Tracker"], ["entity_column", "name"], ["lang", "en"]]
  EasyTranslation Load (1.5ms)  SELECT  "easy_translations".* FROM "easy_translations" WHERE "easy_translations"."entity_id" = $1 AND "easy_translations"."entity_type" = $2 AND "easy_translations"."entity_column" = $3 AND "easy_translations"."lang" = $4  ORDER BY "easy_translations"."id" ASC LIMIT 1  [["entity_id", 4], ["entity_type", "Tracker"], ["entity_column", "name"], ["lang", "en"]]
  EasyTranslation Load (0.7ms)  SELECT  "easy_translations".* FROM "easy_translations" WHERE "easy_translations"."entity_id" = $1 AND "easy_translations"."entity_type" = $2 AND "easy_translations"."entity_column" = $3 AND "easy_translations"."lang" = $4  ORDER BY "easy_translations"."id" ASC LIMIT 1  [["entity_id", 5], ["entity_type", "Tracker"], ["entity_column", "name"], ["lang", "en"]]
2.1.1 :003 > @tra = Tracker.where(name: "+ Camps & Finished Food Store")
  Tracker Load (0.8ms)  SELECT "trackers".* FROM "trackers" WHERE "trackers"."name" = $1  [["name", "+ Camps & Finished Food Store"]]

【问题讨论】:

  • 你为什么使用'+测试'?
  • "+ Test" 不一样"Test"
  • 抱歉,我忘记为检测查询添加“+”。
  • 缺少哪些跟踪器?发现了什么?执行什么 SQL 查询?
  • 我已经用更多细节更新了这个问题。如果我遗漏了什么,请告诉我。

标签: ruby-on-rails ruby redmine


【解决方案1】:

Tracker.where(name: "Permits required")

【讨论】:

    猜你喜欢
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多