views:

56

answers:

0

Greetings,

I have three models

class Patient < ActiveRecord::Base
  has_many :admissions
  accepts_nested_attributes_for :admissions, :reject_if => :all_blank, :allow_destroy => true
end

class Admission < ActiveRecord::Base
  belongs_to :patient
  has_one :admission_info, :dependent => :destroy
  accepts_nested_attributes_for :admission_info, :reject_if => :all_blank, :allow_destroy => true
end

class AdmissionInfo < ActiveRecord::Base
  belongs_to :admission
end

For create everything is working fine. But when I try to edit @patient model, it is automatically setting admission_id to null in the admission_infos table.

I am not doing this manually don't have a clue right now how it is happening automatically.

This is from the log:

...
Patient Load (1.4ms)  SELECT "patients".* FROM "patients" WHERE ("patients"."id" = 6) LIMIT 1
...
Admission Load (1.3ms)  SELECT "admissions".* FROM "admissions" WHERE ("admissions".patient_id = 6) LIMIT 1
...
AdmissionInfo Load (1.4ms)  SELECT "admission_infos".* FROM "admission_infos" WHERE ("admission_infos".admission_id = 6) LIMIT 1
...
CACHE (0.0ms)  SELECT "admissions".* FROM "admissions" WHERE ("admissions".patient_id = 6) LIMIT 1
  CACHE (0.0ms)  SELECT "admission_infos".* FROM "admission_infos" WHERE ("admission_infos".admission_id = 6) LIMIT 1
  SQL (1.4ms)   SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
 FROM pg_attribute a LEFT JOIN pg_attrdef d
 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attrelid = '"admission_infos"'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
 ORDER BY a.attnum
  CACHE (0.0ms)  SELECT "admissions".* FROM "admissions" WHERE ("admissions".patient_id = 6) LIMIT 1
  CACHE (0.0ms)  SELECT "admission_infos".* FROM "admission_infos" WHERE ("admission_infos".admission_id = 6) LIMIT 1
  SQL (0.4ms)  BEGIN
  SQL (1.4ms)   SELECT a.attname, format_type(a.atttypid, a.atttypmod), d.adsrc, a.attnotnull
 FROM pg_attribute a LEFT JOIN pg_attrdef d
 ON a.attrelid = d.adrelid AND a.attnum = d.adnum
 WHERE a.attrelid = '"admission_infos"'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
 ORDER BY a.attnum
  SQL (1.1ms)  UPDATE "admission_infos" SET "admission_id" = NULL, "updated_at" = '2010-09-29 08:38:42.832952' WHERE ("admission_infos"."id" = 6)
  SQL (1.1ms)  COMMIT
...

Any ideas how I can resolve this issue?