【发布时间】:2016-04-22 19:32:23
【问题描述】:
已解决:原来我在 MySQL 中打开了错误的数据库...而且我使用了错误的键(有人发布了一个答案,说该行不在我的数据库中。但如果你想的话,他们会删除它再次发布不接受)
由于某种原因,当存在应返回的记录时,.find_by() 返回 nil。我可以使用 .all() 确认该记录存在于数据库中。当我在 MySQL 提示符中手动运行 SQL 时,我还可以确认该 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
【问题讨论】:
标签: mysql ruby-on-rails ruby ruby-on-rails-4