activescaffold/active_scaffold — Issue #144: activescaffold deleting/recreating child records instead of updating them
Author: davesmylie
Created: 2012-03-06 03:28:18+00:00
State: open

When I edit an existing master/detail relationship, the child records are being deleted and recreated instead of just updated. This breaks data integrity.

For non master/detail relationships updates work as expected.

I'm experiencing this issue on active scaffold 3.2.0 but have tried going back a number of versions and am having the same issue. This is on an oracle database running on rails 3.1.

This is for has_many relationship that I am maintaining through activescaffold. (ItemClassGroup has_many :item_classifications).

If I have a foreign key pointing back to the child record, oracle will raise an exception when active scaffold tries to delete the record, other wise the update succeeds according to the front end, but in the backend, I can the see the record is deleted and then recreated with the same information, but with a higher primary key id.

The models and scaffolds are pretty standard - straight out of the box (excepting the non-standard column name prefixes) :

class ItemClassGroup < ActiveRecord::Base
has_many :item_classifications, :foreign_key => 'itcl_icgr_id', :dependent => :destroy
end

class ItemClassification < ActiveRecord::Base
belongs_to :item_class_group, :foreign_key => 'itcl_icgr_id'
end

There is a log entry below where you can see it deleting and recreating the item_classification. In this case it succeeded, but in any instance where there is a foreign key pointing back to the item_classification the delete will fail and an error is raised.

If I remove the dependent => destroy option on the has_many relationship, it just sets the foreign key to NULL, instead of trying to delete the record, but this also breaks the data integrity.

```ruby
Started POST "/admin/item_class_group/36?_=&subsession=&utf8=%E2%9C%93&value=false" for 192.168.1.170 at 2012-03-06 03:04:03 +0000
Processing by As::Admin::ItemClassGroupController#update as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PT4r9R4PkdNK1LD9nZsh4Ndpgq+TKqUGC/3QiTt3Fcw=", "record"=>{"name"=>"Test", "use_desc_ind"=>"0", "desc"=>"", "eff_dte"=>"21/03/2012", "itcl_parent_id"=>"292", "item_classifications"=>{"434"=>{"name"=>"Child", "desc"=>"ee", "active_ind"=>"1", "itcl_id"=>"434"}}, "parent"=>"9", "active_ind"=>"1", "data_type_ind"=>"LIST"}, "commit"=>"Update", "subsession"=>"", "_"=>"", "value"=>"false", "id"=>"36"}

ItemClassGroup Load (2.3ms) SELECT "ITEM_CLASS_GROUP".* FROM "ITEM_CLASS_GROUP" WHERE "ITEM_CLASS_GROUP"."ICGR_ID" = 36 AND ROWNUM <= 1
ItemClassification Load (2.3ms) SELECT "ITEM_CLASSIFICATION".* FROM "ITEM_CLASSIFICATION" WHERE ("ITEM_CLASSIFICATION".itcl_icgr_id = 36)
AREL (25.1ms) DELETE FROM "ITEM_CLASSIFICATION" WHERE "ITEM_CLASSIFICATION"."ITCL_ID" = 434
ItemClassGroup Load (2.0ms) SELECT "ITEM_CLASS_GROUP".* FROM "ITEM_CLASS_GROUP" WHERE "ITEM_CLASS_GROUP"."ICGR_ID" = 9 AND ROWNUM <= 1
AREL (1.9ms) INSERT INTO "ITEM_CLASSIFICATION" ("ITCL_ICGR_ID", "ITCL_NAME", "ITCL_DESC", "ITCL_ACTIVE_IND", "ITCL_ITCL_PARENT_ID", "ITCL_ID") VALUES (36, 'Child', 'ee', 'Y', NULL, 435)

Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/_messages.html.erb (1.1ms)
Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/_list_record_columns.html.erb (161.9ms)
Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/_action_group.html.erb (8.6ms)
Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/_list_actions.html.erb (21.1ms)
Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/_list_record.html.erb (207.1ms)
Rendered /home/dgs/.rvm/gems/ruby-1.9.3-p125-perf/gems/active_scaffold-3.0.26/frontends/default/views/on_update.js.rjs (230.2ms)
Completed 200 OK in 877ms (Views: 247.4ms | ActiveRecord: 79.6ms)

```

I can trace it thru in the active scaffold to where the delete is being issued, but I cant' work out *why* it is being issued...
(it's coming from the update_save method in lib/active_scaffold/actions/update.rb if that helps)