Saji,
Interesting, that would make sense. You could also do
floods.parts.find_or_initialize_by_name("body") if you don't want to
drop the parts.
Sean
Saji Njarackalazhikam Hameed wrote:
> Sean,
>
> Thanks very much for the advice. On using "create" and then "update",
> I found that duplicate body "parts" were being "created" every time I ran
> the code. So running the script 5.times resulted in 5 "body parts" for the
> same page. After some experiments, here is what I found to suit my
> purposes ( replacing a part of the page with new stuff). Esp. used
> the "clear" method to wipe the page parts clean
>
> # ------------
> floods=Page.find_by_url('/services/news/floods')
> floods.parts.clear # wipe clear the flood page of its parts
> floods.parts.create(
> :content => "Now for floods and droughts",
> :name => "body"
> )
> floods.update
> # ------------
>
> saji
> ....
>
>
> * Sean Cribbs <[EMAIL PROTECTED]> [2007-08-28 08:09:10 -0500]:
>
>
>> 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: [email protected]
>>> Search: http://radiantcms.org/mailing-list/search/
>>> Site: http://lists.radiantcms.org/mailman/listinfo/radiant
>>>
>>>
>>>
>> _______________________________________________
>> Radiant mailing list
>> Post: [email protected]
>> Search: http://radiantcms.org/mailing-list/search/
>> Site: http://lists.radiantcms.org/mailman/listinfo/radiant
>>
>
>
_______________________________________________
Radiant mailing list
Post: [email protected]
Search: http://radiantcms.org/mailing-list/search/
Site: http://lists.radiantcms.org/mailman/listinfo/radiant