Recently I have migrated one of my models from a self made (parent_id based) tree structure to the ancestry gem and I have a failing controller spec for the new
action. The line in question is:
assigns(:category).should be_a_new(Category).with(parent_id: root_category.id)
However it complains that there's no attribute parent_id
and indeed it isn't as parent_id is now a method. How should I modify the above line to check for both a new category and that category.parent_id == root_category.id
?
Instead of 'parent_id' you can use the column name ancestry uses to store the parent's id which is 'ancestry' by default. You can change the name by setting it as option to the has_ancestry
class method, e.g. has_ancestry(ancestry_column: :parent_id)
.
Your modified test code would look as follows:
assigns(:category).should be_a_new(Category).with(ancestry: root_category.id.to_s)