I'm trying to set up a self-referential relationship, as described in this great video - #163 Self-Referential Association - RailsCasts - and it's mostly working, but not entirely.
My self-referential relationship describes relationships between people using the foreign keys person and related_person in class Relationship both associating to the class Person
My problem is that when I update related_person_id for a specific relationship the associated object relationship.related_person is not updated. Related_person_id in relationship has an updated value, while relationship.related_person.id has the old value. What is wrong in my declarations ?? They are as follows
Class Person
has_many :relationships, :dependent=>:destroy, :autosave=>true has_many :people, :through => :relationships, :source=>:person, :uniq=>true has_many :related_people, :through => :relationships, :source=>:related_person, :uniq=>true
Class Relationship belongs_to :person belongs_to :related_person, :class_name => 'Person',:foreign_key =>'related_person_id'
Table relationships class CreateRelationships < ActiveRecord::Migration def self.up create_table :relationships do |t| t.integer :person_id, :default=> nil t.integer :related_person_id, :default=> nil t.string :person_role, :limit=>80 t.string :related_person_role, :limit=>80 t.integer :family_id, :default=> nil t.integer :event_id, :default=> nil t.integer :user_id, :default=> nil t.integer :archive_id, :default=> nil t.timestamps end add_index :relationships, :person_id add_index :relationships, :related_person_id add_index :relationships, :archive_id add_index :relationships, :person_role, :index => true add_index :relationships, :related_person_role, :index => true end