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

Graph like behaviour #3

Open
yamanyar opened this issue May 30, 2011 · 0 comments
Open

Graph like behaviour #3

yamanyar opened this issue May 30, 2011 · 0 comments

Comments

@yamanyar
Copy link

I can store a relation that is a graph in fact. Is it expected? I think it should throw an exception when there is a recycling path (For example A->B->A->B....). Here is the test (please note that this test passes; however i am expecting it to fail):

category = Category.new
category.name="Parent"
category.save
child = category.children.create("name" => "child_1")
child.children = [category]
assert category.save
loadedChild = Category.find_by_name("child_1")
assert_equal "Parent", loadedChild.parent.name
assert_equal "Parent", loadedChild.children[0].name

I added following code snippet to my domain object: (i am new to ruby so may be this is not the best way)

Although this seems alright with unit tests; when i test manually from browser; server dies due to infinite loop i guess.

validate :validate_recycling

def validate_recycling
begin
check_recycling []
rescue ArgumentError => msg
errors.add(:base, msg.message)
end
end

def check_recycling(parents)
raise ArgumentError, "#{self.name} is recycling!" if parents.include?(self)
copy = parents.dup
copy.push self
children.each { |child| child.check_recycling(copy) }
end

I guess the problem is before to validation, following lines (i found in the source code of the gem i installed) may run forever:

nodes << node = node.parent while node.parent
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

1 participant