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 3983148
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:36:47+00:00 2026-05-20T05:36:47+00:00

I have built a site that relies on an XML feed that I currently

  • 0

I have built a site that relies on an XML feed that I currently parse with Nokogiri. Everything works fine and dandy although I have all the code currently within my Admin controller so I can actually invoke the import via a URL i.e. /admin/import/.

I can’t help but think that this doesn’t belong in the controller. Is there a better way to do this, i.e. move the code into a stand alone import.rb file so it is only accessible from the console? If so where would I need to put this file, in the /lib/ directory?

Here is a code snippet:

class AdminController < ApplicationController

    def import
      f = File.open("#{Rails.root}/public/feed.xml")
      @doc = Nokogiri::XML(f)
      f.close

      ignore_list = [] # ignore list

      @doc.xpath("/*/product[not(name = following-sibling::product/name)]").each do |node|
        if !ignore_list.include? node.xpath("./programName").inner_text.strip
          Product.create(:name => clean_field(node.xpath("./name").inner_text).downcase, 
          :description => clean_field(node.xpath("./description").inner_text),
          :brand => Brand.find_or_create_by_name(clean_field_key(node.xpath("./brand").inner_text).downcase),         
          :merchant => Merchant.find_or_create_by_name(clean_field_key(node.xpath("./programName").inner_text).downcase),     
          :image => node.xpath("./imageUrl").inner_text.strip,
          :link => node.xpath("./productUrl").inner_text.strip,
          :category => Category.find_or_create_by_name(clean_field_key(node.xpath("./CategoryName").inner_text).downcase),
          :price => "£" + node.xpath("./price").inner_text.strip)
          print clean_field(node.xpath("./name").inner_text).downcase + "\n"       
        end
      end
    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-05-20T05:36:47+00:00Added an answer on May 20, 2026 at 5:36 am

    Your code sounds like it would work well being run as a Rails runner script. They’re scripts that run outside of the normal Rails process for your site, but have full access to the ActiveRecord setup so you can easily access your database.

    I don’t think Rails is as strict about the file’s location as it is for all its other files, but I’d create a subdirectory under ‘app’ called ‘scripts’ and put it there. Keeping a tidy directory structure is a good thing for maintenance.

    You don’t say if you’re running Rails 3 or a previous version. If you’re running Rails 3, type rails runner -h at the command-line of your Rails app for more info.

    Some folks feel that scripts should be run using rake, which I agree with IF they’re manipulating files and folders and doing general maintenance of the rails-space your app runs in. If you’re performing a periodic task that is part of the housekeeping of the database, or, in your case, retrieving content used in support of your app, I think it should be a “runner” task.

    You can build functionality so you could still trigger the code to run via a URL, but I think there’s potential for abuse of that, especially if you could overwrite needed data or fill the database with duplicate/redundant data. I think it’d be better to make the task run periodically, via cron initiated by the OS probably, just to keep things on a nice interval, or only run manually. If you keep it available via URL access I’d recommend using a password to help avoid abuse.

    Finally, as someone who’s been doing this a long time, I’d recommend a bit of structure and alignment in your code:

    Product.create(
      :name        => clean_field(node.xpath("./name").inner_text).downcase,
      :description => clean_field(node.xpath("./description").inner_text),
      :brand       => Brand.find_or_create_by_name(clean_field_key(node.xpath("./brand").inner_text).downcase),
      :merchant    => Merchant.find_or_create_by_name(clean_field_key(node.xpath("./programName").inner_text).downcase),
      :image       => node.xpath("./imageUrl").inner_text.strip,
      :link        => node.xpath("./productUrl").inner_text.strip,
      :category    => Category.find_or_create_by_name(clean_field_key(node.xpath("./CategoryName").inner_text).downcase),
      :price       => "£" + node.xpath("./price").inner_text.strip
    )
    

    Simple alignment can go a long way to help you maintain your code, or help keep the sanity of someone down the line who ends up maintaining it. I’d probably keep it looking like:

    Product.create(
      :name        => clean_field( node.xpath( "./name"        ).inner_text ).downcase,
      :description => clean_field( node.xpath( "./description" ).inner_text ),
    
      :brand       => Brand.find_or_create_by_name(    clean_field_key( node.xpath( "./brand"       ).inner_text ).downcase ),
      :merchant    => Merchant.find_or_create_by_name( clean_field_key( node.xpath( "./programName" ).inner_text ).downcase ),
    
      :image       => node.xpath( "./imageUrl"   ).inner_text.strip,
      :link        => node.xpath( "./productUrl" ).inner_text.strip,
    
      :category    => Category.find_or_create_by_name( clean_field_key( node.xpath( "./CategoryName" ).inner_text ).downcase ),
    
      :price       => "£" + node.xpath( "./price" ).inner_text.strip
    )
    

    but that’s just me. I like having more whitespace, especially when there are nested methods, and I like having some vertical alignment among common/similar functions. I find it makes it easier to scan the code and see any differences, which helps when you are debugging or looking for a particular thing. Again, that’s just my preference, but it’s something I’ve learned over many years of writing code in a lot of different languages.

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

Sidebar

Related Questions

I have a web site that built on Visual studio 2008 and i need
I have a simple php site that I built, but for some reason my
I have a legacy web-site that I am maintaining (built mostly in classic ASP
I am usuing CKEditor (version 3.0.2) in a site that I built. I have
i have built a beautiful website that works very fast in all of the
I have built a site that when a user fills in a form correctly
I have built a simple site that allows users to share code where the
I have a site that I built with jsp's and I would like to
I have a wordpress site that was built for me by a developer some
I have be asked to build a site that is to be built in

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.