Saji,

You might try using "create" instead of "build". In that case, it could 
look like this:

parent = Page.find_by_url('/services/news')
child = parent.children.create(:title => "Floods", :breadcrumb => 
"Floods", :slug => "floods")
child.parts.create(:name => "body", :content => "This is a page for 
flood news")

Keep in mind that create and build might not work if the collection is 
empty or the original object is unsaved [1]. Your other alternative is 
to build the objects manually, then push it into the collection:

parent = Page.find_by_url('/services/news')
child = Page.new(:title => "Floods", :breadcrumb => "Floods", :slug => 
"floods")
part = PagePart.new(:name => "body", :content => "This is a page for 
flood news")
parent.children << child
child.parts << part

Also note in your second snippet that your part needs a name.

Sean

[1] 
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000642
 
(if I'm interpreting the doc correctly)

Saji Njarackalazhikam Hameed wrote:
> Dear All,
>
> Thanks for the kind lessons on how to add a page using script/runner. I
> have the following working script to create such a new page:
>
> # ------ start of script to add a new page ---
>
>  parent = Page.find_by_url('/services/news')
>    child = parent.children.build(:title => "Floods",
>    :breadcrumb => "Floods", :slug => "floods") 
>    child.parts.build(:content => "This is a page for flood news",
>    :name    => "body")
>    child.save!
>
> # ------ end of script to add a new page ---
>
> How may I update the contents using script/runner? I have tried
> the following, but it does not work. Any help to solve this
> would be much appreciated.
>
> # ---- test script that does not work
>
>  floods = Page.find_by_url('/services/news/floods')
>    floods.parts.build(:content => "This is now for droughts")
>    floods.update
>
> thanks in advance,
>
> saji
>
> _______________________________________________
> Radiant mailing list
> Post:   Radiant@lists.radiantcms.org
> Search: http://radiantcms.org/mailing-list/search/
> Site:   http://lists.radiantcms.org/mailman/listinfo/radiant
>
>   

_______________________________________________
Radiant mailing list
Post:   Radiant@lists.radiantcms.org
Search: http://radiantcms.org/mailing-list/search/
Site:   http://lists.radiantcms.org/mailman/listinfo/radiant

Reply via email to