denodster denodster - 7 months ago 6
Ruby Question

find_by() returns nil when database has the record

For some reason .find_by() is returning nil when a record exists that should be returned. I can confirm that the record exists in the DB using .all() I can also confirm the SQL is returning a row when i run it manually in a MySQL prompt.

Ive checked for a default scope on the Institutions model and there is none.

Heres the output from my debugger and a test database:

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


Once again, if I run this query manually I get the appropriate row.....

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


SOLVED: Turns out I had the wrong Database open in MySQL... and I was using the wrong key (someone posted an answer saying the row wasn't in my database. but then they deleted it, if you want to post it again ill accept)

Answer

You actually have not this record in database since your Institution.where(lti_key: request_key) returns empty relation.

Institution.all.first just returns first record from database without any condition.