当数据库有记录时,find_by() 返回 nil



已解决:原来我在MySQL中打开了错误的数据库...我使用了错误的键(有人发布了一个答案,说该行不在我的数据库中。 但后来他们删除了它,如果你想再次发布它,请接受)

出于某种原因,当存在应返回的记录时,.find_by() 返回 nil。我可以使用 .all() 确认该记录存在于数据库中,当我在 MySQL 提示符下手动运行它时,我还可以确认 SQL 返回一行。

我检查了机构模型的默认范围,但没有。

这是我的调试器和测试数据库的输出:

0> Institution.all
  Institution Load (1.5ms)  SELECT `institutions`.* FROM `institutions`
=> #<ActiveRecord::Relation [#<Institution id: 1, institution_name: "Example Inc.", customer_id: nil, address1: "xxxxxxxx", address2: "", city: "Detroit", zip: "xxxxx", region: "Michigan", country: "United States", zoho_customer_id: nil, lms_id: nil, last_billed_at: nil, lti_key: "3--4a9c711efad7d997d3b2145a2efa14a30d9e91df5bc02f5...", lti_secret: "d439358b8e38e95cb70e", custom_registration: nil, staging: false, lti_return_url: nil, domain: nil, first_name: "xxxxx", last_name: "xxxxxx", email: "xxxxxxx@example.com", phone: nil, uuid: "1ce1f720-5cc7-4abd-9ff5-6a968c1cbd73", standalone: true, logo_url: nil, hex_color_one: "000000", hex_color_two: "FFFFFF", lms_name: "none", slug: nil, created_at: "2016-04-13 15:55:01", updated_at: "2016-04-13 15:55:01", large_logo_url: nil, student_access_after_course: true, no_discussion: false, no_messaging: false, support_email: nil, no_print_and_download: true>]>
0> Institution.where(lti_key: request_key)
  Institution Load (1.0ms)  SELECT `institutions`.* FROM `institutions`  WHERE `institutions`.`lti_key` = '3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d055878d'
=> #<ActiveRecord::Relation []>
0> Institution.all
  CACHE (0.0ms)  SELECT `institutions`.* FROM `institutions`
=> #<ActiveRecord::Relation [#<Institution id: 1, institution_name: "Example Inc.", customer_id: nil, address1: "xxxxxxx", address2: "", city: "Detroit", zip: "xxxxx", region: "Michigan", country: "United States", zoho_customer_id: nil, lms_id: nil, last_billed_at: nil, lti_key: "3--4a9c711efad7d997d3b2145a2efa14a30d9e91df5bc02f5...", lti_secret: "d439358b8e38e95cb70e", custom_registration: nil, staging: false, lti_return_url: nil, domain: nil, first_name: "xxxxxx", last_name: "xxxxxx", email: "xxxxxx@example.com", phone: nil, uuid: "1ce1f720-5cc7-4abd-9ff5-6a968c1cbd73", standalone: true, logo_url: nil, hex_color_one: "000000", hex_color_two: "FFFFFF", lms_name: "none", slug: nil, created_at: "2016-04-13 15:55:01", updated_at: "2016-04-13 15:55:01", large_logo_url: nil, student_access_after_course: true, no_discussion: false, no_messaging: false, support_email: nil, no_print_and_download: true>]>
0> Institution.all.first
  Institution Load (1.0ms)  SELECT  `institutions`.* FROM `institutions`   ORDER BY `institutions`.`id` ASC LIMIT 1
=> #<Institution id: 1, institution_name: "Example Inc.", customer_id: nil, address1: "xxxxxxx", address2: "", city: "Detroit", zip: "xxxxx", region: "Michigan", country: "United States", zoho_customer_id: nil, lms_id: nil, last_billed_at: nil, lti_key: "3--4a9c711efad7d997d3b2145a2efa14a30d9e91df5bc02f5...", lti_secret: "d439358b8e38e95cb70e", custom_registration: nil, staging: false, lti_return_url: nil, domain: nil, first_name: "xxxxxx", last_name: "xxxxxxx", email: "xxxxxxxx@example.com", phone: nil, uuid: "1ce1f720-5cc7-4abd-9ff5-6a968c1cbd73", standalone: true, logo_url: nil, hex_color_one: "000000", hex_color_two: "FFFFFF", lms_name: "none", slug: nil, created_at: "2016-04-13 15:55:01", updated_at: "2016-04-13 15:55:01", large_logo_url: nil, student_access_after_course: true, no_discussion: false, no_messaging: false, support_email: nil, no_print_and_download: true>
0> request_key
=> "3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d055878d"
0> Institution.find_by( lti_key: request_key )
  CACHE (0.1ms)  SELECT  `institutions`.* FROM `institutions`  WHERE `institutions`.`lti_key` = '3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d055878d' LIMIT 1
=> nil

再一次,如果我手动运行此查询,我会得到相应的行.....

SELECT  `institutions`.* FROM `institutions`  WHERE `institutions`.`lti_key` = '3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d055878d' LIMIT 1

您正在请求"3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d0558..."并且该行"3--4a9c711efad7d997d3b2145a2efa14a30d9e91df5bc02f5..."

由于您的Institution.where(lti_key: request_key)返回空关系,您实际上没有在数据库中查看此记录。

Institution.all.first 只是从数据库返回第一条记录,没有任何条件。

相关内容

  • 没有找到相关文章

最新更新