Add Children to Node in Seed File


(Justin Joyce) #1

Good Morning from Austin, Texas!

In my seed file I create a Page node called titled About Us.

Page.create!(
{ title: "About Us",
  slug: "about",
  description: "About Company",
  status: PushType::Node.statuses[:published] }
 )

I would like to also generate a child node off of About Us. I am not sure if I need to pass a hash option on a field type or if it is similar to adding structure like so:

LandingPage.create!([
{ title: "Home",
  slug: "home",
  hero: {
    title: 'blah,
    subtitle: 'blah.',
    buttontext: 'blah',
    buttonurl: 'https://blah.com/'
  },
  status: PushType::Node.statuses[:published] }
])

Thank you!


(Aaron Russell) #2

Hi @justinwjoyce

Internally the PushType::Node class uses a normal rails associaions for the tree-like heirachy relationships (using the closure_tree gem). So a node belongs_to :parent and has_many :children.

The way I’d approach what you’re doing is this:

parent = Page.create!({
  title: "About us",
  ...
})

LandingPage.create({
  title: "Child page",
  parent: parent,
  ...
})

Hope that helps :)


(Justin Joyce) #3

That was very helpful. Thank you sir and great work on this CMS solution!