I have two models with a simple one-to-many relationship. I’m writing a rake task where the parent is created and I am attempting to find one of its children. When a parent is created there is a after_create
callback that creates default child objects. But when trying to find the child record by its ID (via Child.find method), rails returns RecordNotFound. Even the parent cannot be found by its ID even though the created record has an ID and calling persisted?
returns true.
I’m not quite sure if this is a problem with objects created within a rake task that aren’t immediately committed to the database or something else. Any pointers would be greatly appreciated.
(byebug) ap eefpst1r.result_statistic_sections
[
[0] #<ResultStatisticSection:0x00007f1349908ce8> {
:id => 734132,
:result_statistic_section_type_id => 1,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[1] #<ResultStatisticSection:0x00007f13497f9ff0> {
:id => 734133,
:result_statistic_section_type_id => 2,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[2] #<ResultStatisticSection:0x00007f13496a03e8> {
:id => 734134,
:result_statistic_section_type_id => 3,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[3] #<ResultStatisticSection:0x00007f134955bde8> {
:id => 734135,
:result_statistic_section_type_id => 4,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[4] #<ResultStatisticSection:0x00007f1349432548> {
:id => 734136,
:result_statistic_section_type_id => 5,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[5] #<ResultStatisticSection:0x00007f13492eb248> {
:id => 734137,
:result_statistic_section_type_id => 6,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[6] #<ResultStatisticSection:0x00007f13492b8aa0> {
:id => 734138,
:result_statistic_section_type_id => 7,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
},
[7] #<ResultStatisticSection:0x00007f1349291e00> {
:id => 734139,
:result_statistic_section_type_id => 8,
:population_id => 92469,
:created_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00,
:updated_at => Thu, 15 Dec 2022 13:29:00.000000000 UTC +00:00
}
]
nil
(byebug) ResultStatisticSection.find 734132
*** ActiveRecord::RecordNotFound Exception: Couldn't find ResultStatisticSection with 'id'=734132
(byebug) ap eefpst1r.result_statistic_sections.map(&:persisted?)
[
[0] true,
[1] true,
[2] true,
[3] true,
[4] true,
[5] true,
[6] true,
[7] true
]
nil