Rails refuses to add associated record

I have a table with 2 foreign key relationships - to a Timeline and a Phase. When I create a record in my development database it all works 100% as expected, but when I do it in test mode it refuses to add the Timeline - you can see from the INSERT statement that it flatly refuses .. it doesn't even try to add it. When I run the exact same sequence below in development it's fine

I can add/update timeline_id but then it doesn't reference the timeline through the parent phase_timeline object as it should. I repeat that this all works fine in development, but not in test. Its driving me mad. Is it failing a validation possibly.. or could the database be corrupt. Are there some console commands I could run to check out the foreign key relationship further?

[33] pry(main)> t = Timeline.last
  Timeline Load (0.3ms)  SELECT  "timelines".* FROM "timelines" ORDER BY "timelines"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Timeline:0x0055fd716dcfa8 id: 1, title: "England", timelineable_type: "Global", timelineable_id: 1, created_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00, updated_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00>
[34] pry(main)> p = Phase.last
  Phase Load (1.3ms)  SELECT  "phases".* FROM "phases" WHERE "phases"."deleted_at" IS NULL ORDER BY "phases"."id" DESC LIMIT $1  [["LIMIT", 1]]
=> #<Phase:0x0055fd717f8450
 id: 1,
 name: "First phase",
 development_id: 1,
 created_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00,
 updated_at: Thu, 24 Sep 2020 14:46:28 UTC +00:00,
 developer_id: 1,
 division_id: 1,
 number: 1,
 deleted_at: nil,
 total_snags: 0,
 unresolved_snags: 0,
 business: "core">
[35] pry(main)> pt = PhaseTimeline.create(phase: p, timeline: t)
   (0.2ms)  BEGIN
  SQL (0.5ms)  **INSERT INTO "phase_timelines" ("phase_id") VALUES ($1) RETURNING "id"  [["phase_id", 1]]**
   (1.8ms)  COMMIT
=> #<PhaseTimeline:0x0055fd719ef9c0 id: 5, phase_id: 1, timeline_id: nil>

1 answer

  • answered 2020-09-25 09:20 user1587804

    After a LOT of head scratching and diving into the bowels .. this problem was caused by having 2 model classes with the same name. The classes were in 2 separate folder but had the same scope and were identical but removing the errant one sorted the problem