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