# Has one relationship through join table - orphaned records on update

**URL:** <https://www.ruby-forum.com/t/has-one-relationship-through-join-table-orphaned-records-on-update/186712>\
**Category:** Rails\
**Created:** [April 12, 2010, 2:42am UTC](https://www.ruby-forum.com/t/has-one-relationship-through-join-table-orphaned-records-on-update/186712 "2010-04-12T02:42:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbkbali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@dbkbali](https://www.ruby-forum.com/u/dbkbali)\
**Post date:** [April 12, 2010, 2:42am UTC](https://www.ruby-forum.com/t/has-one-relationship-through-join-table-orphaned-records-on-update/186712/1 "2010-04-12T02:42:34Z")

</div>

I have set up the following relationships Rails 2.3.5 and on update I  
am finding that orphaned records are generated. Are my relationships  
wrong or is there a recommended way to manage these orphaned records  
on an update. Code is detailed below:

class User

has\_many :management\_assignments, :foreign\_key =\> :manager\_id  
has\_many :listings, :through =\> :management\_assignments  
…

class ManagementAssignment \< ActiveRecord::Base

attr\_accessible :manager\_id, :id, :listing\_id, :assignor\_id, :status,  
:assignment\_date, :completed\_date  
belongs\_to :manager, :class\_name =\> ‘User’  
belongs\_to :assignor, :class\_name =\> ‘User’  
belongs\_to :listing

class Listing  
has\_one :management\_assignment, :dependent  
=\> :destroy  
has\_many :managers, :through =\> :management\_assignments, :source  
=\> :user

accepts\_nested\_attributes\_for :management\_assignment, :allow\_destroy  
=\> true

My update controller action is a bulk update which uses a nested form  
to change the manager on the relationship as follows:

def update\_multiple  
@listings = Listing.find(params[:listing\_ids])  
@listings.each do |listing|  
listing.last\_updated\_by = current\_user  
listing.update\_attributes!(params[:listing].reject { |k,v|  
v.blank? })  
end  
flash[:notice] = “Updated listings!”  
redirect\_to admin\_listings\_path  
end

As a noob any help or feedback as to how to eliminate these orphaned  
records would be much appreciated.

Thx.

David

---

<div class="post-metadata">

**Author:** ![dbkbali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cc9497/32.png) [@dbkbali](https://www.ruby-forum.com/u/dbkbali)\
**Post date:** [April 12, 2010, 5:59am UTC](https://www.ruby-forum.com/t/has-one-relationship-through-join-table-orphaned-records-on-update/186712/2 "2010-04-12T05:59:51Z")

</div>

Interestingly when editing the records individually no orphaned  
records are created. This would appear to have something to do with  
the Params hash being returned to the server and the nested fields  
for.

The params hash being returned for a bulk update is detailed below:

Parameters: {“listing\_ids”=\>[“1”, “2”, “3”], “commit”=\>“Submit”,  
“action”=\>“update\_multiple”, “\_method”=\>“put”,  
“authenticity\_token”=\>“WaMhK/3aAWkgxB5z5ExV3rSfr9bDEJXjGLoqWYwn8oc=”,  
“listing”=\>{“zhtranslator\_id”=\>"", “frtranslator\_id”=\>"",  
“staff\_assignment\_attributes”=\>{“staff\_id”=\>“3”,  
“assignment\_date”=\>“2010-04-12”, “status”=\>“editing”,  
“assignor\_id”=\>“1”},  
“management\_assignment\_attributes”=\>{“assignment\_date”=\>“2010-04-12”,  
“manager\_id”=\>“2”, “assignor\_id”=\>“1”}, “jptranslator\_id”=\>"",  
“kotranslator\_id”=\>"", “detranslator\_id”=\>"", “idtranslator\_id”=\>“3”,  
“ownership\_attributes”=\>{“owner\_id”=\>""}}, “controller”=\>“admin/  
listings”}

For an individual edit/update the management\_assignment\_attributes  
hash contains the listing id (ie “id” =\> “4”) whereas there are no ids  
in the management\_assignment params hash for a bulk update, so my  
sense is that I somehow have to get the listing ids inside this params  
hash to avoid the orphaned record problem. Any ideas would be much  
appreciated.
