dbkbali
2010-Apr-12 00:41 UTC
has one relationship through join table - orphaned records on update
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
dbkbali
2010-Apr-12 03:58 UTC
Re: has one relationship through join table - orphaned records on update
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. On Apr 12, 8:41 am, dbkbali <dbkr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.