Skip to content

Commit a35b612

Browse files
committed
closes issue 64
1 parent ce25cf1 commit a35b612

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

lib/closure_tree/model.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ module Model
2020
:dependent => _ct.options[:dependent],
2121
:inverse_of => :parent)
2222

23-
2423
has_many :ancestor_hierarchies, *_ct.has_many_without_order_option(
2524
:class_name => _ct.hierarchy_class_name,
2625
:foreign_key => "descendant_id",
@@ -49,12 +48,14 @@ def _ct
4948

5049
# Returns true if this node has no parents.
5150
def root?
52-
_ct_parent_id.nil?
51+
# Accessing the parent will fetch that row from the database,
52+
# so if we are persisted, just check that the parent_id column is nil.
53+
persisted? ? _ct_parent_id.nil? : parent.nil?
5354
end
5455

5556
# Returns true if this node has a parent, and is not a root.
5657
def child?
57-
!parent.nil?
58+
!root?
5859
end
5960

6061
# Returns true if this node has no children.

spec/label_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ def create_preorder_tree(suffix = "")
8686

8787
context "Parent/child inverse relationships" do
8888
it "should associate both sides of the parent and child relationships" do
89-
parent = Label.new(:name => '123')
90-
child = parent.children.build
89+
parent = Label.new(:name => 'parent')
90+
child = parent.children.build(:name => 'child')
9191
parent.should be_root
9292
parent.should_not be_leaf
9393
child.should_not be_root

0 commit comments

Comments
 (0)