Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self_and_ancestors - need for reload of model #68

Closed
istana opened this issue Jul 10, 2013 · 4 comments
Closed

self_and_ancestors - need for reload of model #68

istana opened this issue Jul 10, 2013 · 4 comments

Comments

@istana
Copy link

istana commented Jul 10, 2013

Hi, it is quite counterintuitive, that 'self_and_ancestors' doesn't see changes to node until reload. Is it targeted behaviour and I should use reload in methods, which use these types of scopes?

x = Vega::Category.create(name: 'Parent2')
=> #<Vega::Category id: 3, name: "Parent2", position: 100, uri: "parent2", parent_id: nil, created_at: "2013-07-10 14:16:39", updated_at: "2013-07-10 14:16:39">

x.self_and_ancestors.to_a
=> []

x.reload
=> #<Vega::Category id: 3, name: "Parent2", position: 100, uri: "parent2", parent_id: nil, created_at: "2013-07-10 14:16:39", updated_at: "2013-07-10 14:16:39">

x.self_and_ancestors.to_a
=> [#<Vega::Category id: 3, name: "Parent2", position: 100, uri: "parent2", parent_id: nil, created_at: "2013-07-10 14:16:39", updated_at: "2013-07-10 14:16:39">]
@mceachen
Copy link
Collaborator

I can't reproduce your issue on either Rails 3.2 or Rails 4.0:

~/code/closure_tree (master) $ DB=mysql irb -I "lib:spec"
irb(main):001:0> require 'spec_helper'
irb(main):002:0> t = Tag.create(:name => 'hello')
=> #<Tag id: 1051683930, name: "hello", title: nil, parent_id: nil, sort_order: nil, created_at: "2013-07-20 09:22:45", updated_at: "2013-07-20 09:22:45">
irb(main):003:0> t.self_and_ancestors.to_a
=> [#<Tag id: 1051683930, name: "hello", title: nil, parent_id: nil, sort_order: nil, created_at: "2013-07-20 09:22:45", updated_at: "2013-07-20 09:22:45">]

If I had to guess, you've got a validation that reads self_and_ancestors before the entity has been persisted. You might want to guard that validation with a :if => :persisted?.

@mceachen mceachen reopened this Jul 20, 2013
@mceachen
Copy link
Collaborator

I thought about this more, and realized that if you reparent a node, the self_and_ancestors isn't reset properly. I'll add that now.

mceachen added a commit that referenced this issue Jul 20, 2013
@istana
Copy link
Author

istana commented Jul 20, 2013

that's good, thanks!

@mceachen
Copy link
Collaborator

Fixed in v4.2.7. Tell me how things go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants