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

Already on GitHub? Sign in to your account

children.create does not inherit parent relations #71

JeanMertz opened this Issue Sep 8, 2011 · 2 comments


None yet
2 participants

Not sure how to call this, so the title might look a bit weird, but here is a short rails console example:

$ parent = Theme.first.pages.create(name: "parent")
 => #<Page id: 1, name: "parent", position: 1, ancestry: nil, ancestry_depth: 0, theme_id: 19>
$ parent.children
  Page Load (0.4ms)  SELECT "pages".* FROM "pages" WHERE "pages"."ancestry" = '1' ORDER BY position ASC
 => []
$ child = parent.children.create(name: "child")
 => #<Page id: 2, name: "child", position: 1, ancestry: "1", ancestry_depth: 1, theme_id: nil>
$ child.theme
  Theme Load (0.5ms)  SELECT "themes".* FROM "themes" WHERE "themes"."id" IS NULL LIMIT 1
 => nil 

I expected theme_id to be set the same as the parent. Is there something I am missing here? Or am I supposed to always do parent.children.create(name: "child", theme: parent.theme)?

moiristo commented Sep 9, 2011

I don't think it's logical that ancestry should take care of things like that; it is only meant to maintain a hierarchy. In your case, I'd add a before_create filter in Page to set the theme_id to the parent's theme_id (if any).

@moiristo moiristo closed this Sep 9, 2011

I see your point, and I guess a before_create filter would easily do the job. I was somehow expecting it to work like theme.pages.create works, where the new page takes the theme_id of the referenced theme due to the associations made in Rails.

Anyway, thanks for the reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment