Hans
2011-Jun-21 08:27 UTC
Self referential has many through - belong to association dows not work ???
I''m trying to set up a self-referential relationship, as described in this great video - http://railscasts.com/episodes/163-self-referential-association - 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 -- 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.
Andrew Skegg
2011-Jun-21 23:12 UTC
Re: Self referential has many through - belong to association dows not work ???
Hans <Hans.Marmolin@...> writes:> > I''m trying to set up a self-referential relationship, as described in > this great video - http://railscasts.com/episodes/163-self-referential-association> - 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''The code looks OK to me. Are you sure it''s not a caching/stale record problem? Fire up a console and execute your code manually. If you see the same error try reloading the instance, eg: person.reload This will ensure you are looking at a current version from the database rather than a (possibly) stale version in Ruby memory. -- 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.
Matt Jones
2011-Jun-22 11:29 UTC
Re: Self referential has many through - belong to association dows not work ???
On Jun 21, 4:27 am, Hans <Hans.Marmo...-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote:> I''m trying to set up a self-referential relationship, as described in > this great video -http://railscasts.com/episodes/163-self-referential-association > - 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 ??Nothing, but updating the related_person_id field is the problem. You''ll typically want to update the relationship with an object (assigning to related_person). --Matt Jones -- 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.
Hans
2011-Jun-22 13:42 UTC
Re: Self referential has many through - belong to association dows not work ???
Thanks for the advices Matt do you mean that I should use relationship.related_person=Person.new instead of changing the foreign key directly If so, is that a general requirement when updating foreign keys ? Andrew How do I avoid the caching/stale record process I have tried to make an extra save for all involved object but did not change anything On 22 Juni, 13:29, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 21, 4:27 am, Hans <Hans.Marmo...-6LjvI5LOC4niH4Lt12DN6A@public.gmane.org> wrote: > > > I''m trying to set up a self-referential relationship, as described in > > this great video -http://railscasts.com/episodes/163-self-referential-association > > - 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 ?? > > Nothing, but updating the related_person_id field is the problem. > You''ll typically want to update the relationship with an object > (assigning to related_person). > > --Matt Jones-- 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.
Andrew Skegg
2011-Jun-22 23:25 UTC
Re: Self referential has many through - belong to association dows not work ???
Hans <Hans.Marmolin@...> writes:> > Thanks for the advices > > Matt do you mean that I should use > relationship.related_person=Person.new instead of changing the foreign > key directly > If so, is that a general requirement when updating foreign keys ?I think he means: person = Person.first other_person = Person.last person.relationships << other_person or person.relationships << Person.new or even better person.relationships << Person.create(params[:person])> > Andrew > How do I avoid the caching/stale record process > I have tried to make an extra save for all involved object but did not > change anythingStale objects can be a fickle affair and there are no magic bullets. Sorry. -- 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.