已解决:原来我在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
答案 0 :(得分:3)
您要求"3--5ac4090c6facdc5b795f64da9b70a5351cb60e86a301a820d94a83f8d0558..."
并且行是"3--4a9c711efad7d997d3b2145a2efa14a30d9e91df5bc02f5..."
答案 1 :(得分:1)
由于Institution.where(lti_key: request_key)
返回空关系,因此您实际上没有在数据库中记录此记录。
Institution.all.first
只返回数据库中的第一条记录而没有任何条件。