Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9174639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:47:08+00:00 2026-06-17T16:47:08+00:00

I am trying to create a loop to create a record in my database

  • 0

I am trying to create a loop to create a record in my database for every content_option. I have it so it will create one option, but not the other and I’m not sure why. I tried copying other code that I have found on google, but it all only does 1 entry.

    <?xml version="1.0" encoding="UTF-8" ?>
        <event>
            <name>My Event</name>
            <legacy_id>54321</legacy_id>
            <domain_id>1</domain_id>
            <description>This is a Auction</description>
            <event_type></event_type>
            <starts_at></starts_at>
            <street_1></street_1>
            <city></city>
            <state></state>
            <zip></zip>
            <country></country>
            <phone_1></phone_1>
            <fax></fax>
            <content_options>
                <content_option>
                    <contentable_type>Event</contentable_type>
                    <contentable_id>54321</contentable_id>
                    <key>terms_and_conditions</key>
                    <content>Some Terms and Conditions</content>
                </content_option>
                <content_option>
                    <contentable_type>Event</contentable_type>
                    <contentable_id>54321</contentable_id>
                    <key>email</key>
                    <content>event@event.com</content>
                </content_option>
            </content_options>
            <event_links>
                <link>
                    <name>Brochure</name>
                    <event_id>54321</event_id>
                    <uri>http://www.mysite.com</uri>
                </link>
                <link>
                    <name>Auction Documents</name>
                    <event_id>54321</event_id>
                    <uri>http://www.auctiondocuments.com</uri>
                </link>
            </event_links>
            <phone_numbers>
                <phone_number>
                    <number_type>Approvals</number_type>
                    <event_id>54321</event_id>
                    <name></name>
                    <number></number>
                </phone_number>
                <phone_number>
                    <number_type></number_type>
                    <event_id></event_id>
                    <name></name>
                    <number></number>
                </phone_number>
            </phone_numbers>
            <lots>
                <lot>
                    <legacy_id>12345</legacy_id>
                    <number>1</number>
                    <title>Big Cow</title>
                    <description>A big cow</description>
                    <position>1</position>
                    <price>500</price>
                    <updates>
                        <update>
                            <lot_id>12345</lot_id>
                            <details>Color is blue</details>
                        </update>
                    </updates>
                    <images>
                        <image>http://the.image.is/here.jpg</image>
                    </images>
                    <catalog_images>
                        <catalog_image>http://the.image.is/here.jpg</catalog_image>
                    </catalog_images>
                    <videos>
                        <video>http://the.video.is/here.flv</video>
                    </videos>
                </lot>
            </lots>
        </event>

rails controller

def upload_xml
        xml = params[:file].read
        xmldoc = Nokogiri::XML(xml)

        @event = Event.find_by_legacy_id(xmldoc.xpath("event/legacy_id").text) 
        if @event.nil?
            @event = Event.new 
            @event.legacy_id = xmldoc.xpath("event/legacy_id").text
            @event.name = xmldoc.xpath("event/name").text
            @event.domain_id = xmldoc.xpath("event/domain_id").text
            @event.description = xmldoc.xpath("event/description").text
            @event.event_type = xmldoc.xpath("event/event_type").text
            @event.starts_at = xmldoc.xpath("event/starts_at").text
            @event.street_1 = xmldoc.xpath("event/street_1").text
            @event.city = xmldoc.xpath("event/city").text
            @event.state = xmldoc.xpath("event/state").text
            @event.zip = xmldoc.xpath("event/zip").text
            @event.country = xmldoc.xpath("event/country").text
            @event.phone_1 = xmldoc.xpath("event/phone_1").text
            @event.fax = xmldoc.xpath("event/fax").text
            @event.save

            content_options = xmldoc.xpath("//content_options")

            content_options.each do |option|
                @content_option = ContentOption.new
                @content_option.contentable_type = option.at("contentable_type").text
                @content_option.contentable_id = option.at("contentable_id").text
                @content_option.key = option.at("key").text
                @content_option.content = option.at("content").text
                @content_option.save
            end

        else



        end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:47:09+00:00Added an answer on June 17, 2026 at 4:47 pm

    Change this line of code:

    content_options = xmldoc.xpath("//content_options")
    

    to this:

    content_options = xmldoc.xpath("//content_option")
    

    Of course it will only show you one entry; in your XML, there’s only one content_options element, and there’s 2 content_option elements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a loop that creates a series of objects that contains
I'm trying to create a loop than continues to take input until the input
I am trying to create a loop function based on the time. After the
New to javascript and such, trying to create a loop for fading logos using
I'm trying to create a for loop with NSDates, so that the loop would
I am running a loop, and I am trying to create a variable each
I'm trying to loop through a number of items, and create a json object.
I am tryıng to create new object of other class ın a for loop.
I'm trying to export a record set into Excel but it seems to keep
I'm trying to create a loop to avoid copy pasting these lines 30 times.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.