Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

children.create does not inherit parent relations #71

Closed
JeanMertz opened this Issue · 2 comments

2 participants

@JeanMertz

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

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
@JeanMertz

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
Something went wrong with that request. Please try again.