Hi and thanks for ancestry,
Everything is working great in the console but I am getting an undefined method when I try to render this on my index page from within a partial:
"undefined method `name' for nil:NilClass
<%= category.parent.name %>
It does render the parent_id successfully.
<%= category.parent_id %>
In the console I get this, it returns the parent category name:
>> category = Category.find_by_id(2)
=> <Category id: 2, name: "Male", ancestry: "1">
What am I doing wrong here?
which version of rails are you using?
This is a bug in rails where conditions are applied when they shouldn't be.
Take a look in your console at the query that is produced when calling category.parent.name ; the conditions that you used to find category are applied along with the condition that selects on category.parent_id.
I reported this issue to the Rails Lighthouse at some point but they told me it would be fixed in Rails 3 when converting to Arel.
Perhaps if you submit the issue again and get some people behind it. I know for a fact that moiristo has the same problem.
If that doesn't work we could start looking into building workarounds into ancestry.
Issue 44 #44 is on similar issues and also proposes a solution. For now you can apply this yourself. I will consider and test adding it to ancestry next time I find time to properly work on it.
Resolved on master in cd268b6 and e552e10