Acts_as_tree - parent reference is outdated?

I’m experiencing some unexpected weirdness with acts_as_tree.

class Category < ActiveRecord::Base
acts_as_tree
has_many :variations
end

def variations

some code that returns the parent category variations if we have

none
end

pc = Category.new
pc.variations << “foo”
pc.save
cc = Category.new
cc.parent = pc
cc.save

So far so good …

pc.variations … [“foo”]
cc.variations … [“foo”]

pc.variations.clear

pc.variations … [] <— that’s expected
cc.variations … [“foo”] <-- UNEXPECTED!!
cc = Category.find 2
cc.variations … [] <-- again, expected

What’s going on here? The reference to pc and cc.parent are also not
equal (before reloading even) but I guess that’s just how AR works.
I’m used to Hibernate returning the same reference.

Any suggestions or explanations?

TIA,

Sean

What’s going on here? The reference to pc and cc.parent are also not
equal (before reloading even) but I guess that’s just how AR works.
I’m used to Hibernate returning the same reference.

Any suggestions or explanations?

TIA,

Sean

there must be a problem with your variations method. doublecheck that
area.

Are you sure its not just that the reference to parent is out-dated?
Maybe this is just a feature of ActiveRecord? If belongs_to type
relationships are just copies of what’s in the database at the time
that would explain it. It would also explain why re-retrieving the
record produces the expected result.

Sean

On Oct 5, 7:33 am, Francis S. [email protected]

schof wrote:

Are you sure its not just that the reference to parent is out-dated?
Maybe this is just a feature of ActiveRecord? If belongs_to type
relationships are just copies of what’s in the database at the time
that would explain it. It would also explain why re-retrieving the
record produces the expected result.

Sean

On Oct 5, 7:33 am, Francis S. [email protected]

reload the parent of cc first.

pc.variations.clear
pc.variations … [] <— that’s expected
cc.parent.reload
cc.variations

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs