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

  • Home
  • SEARCH
  • 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 3878870
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:39:42+00:00 2026-05-19T22:39:42+00:00

Ok so I’m starting on normalising my database. Currently I have one model Products

  • 0

Ok so I’m starting on normalising my database. Currently I have one model “Products” which is populated with about 60,000 products via a data feed (XML), which contains a product with a category name and a merchant name. I want to split these into 3 models; products, categories and merchants.

Each product has one category and one merchant so the natural idea is to create these models:

category_id | category_name

merchant_id | merchant_name

I can work out the code to associate between the models i.e. has_one, belongs_to etc but I’m struggling to work out to automatically associate a new Product with a category and a merchant programatically.

I’ve seen examples in books where your start with an empty database and that seems pretty straightforward. However, I’m starting off with a full database and a list of Category names.

Here is my product creation statement which is working great:

Product.create(:name => node.xpath("./text/name/text()").inner_text.downcase, 
                   :description => node.xpath("./text/desc/text()").inner_text,
                   :brand => node.xpath("./brand/text()").inner_text,
                   :merchant => node.xpath("../@name").inner_text,
                   :category => node.xpath("./cat/text()").inner_text.downcase,
                   :price => "£" + node.xpath("./price/btext()").inner_text)

Would I need to do something like this, see the :category line, (i know the following is wrong btw!)…

Product.create(:name => node.xpath("./text/name/text()").inner_text.downcase, 
                   :description => node.xpath("./text/desc/text()").inner_text,
                   :brand => node.xpath("./brand/text()").inner_text,
                   :merchant => node.xpath("../@name").inner_text,
                   :category => << Category.find_by_name(node.xpath("./cat/text()").inner_text.downcase),
                   :price => "£" + node.xpath("./price/btext()").inner_text)

Any ideas? Does this even make sense!?

  • 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-19T22:39:42+00:00Added an answer on May 19, 2026 at 10:39 pm

    Assuming the columns are called category_name and merchant_name, and you’ve set up the associations on Category and Merchant, you could do something like this:

    Product.all do |product|
      product.category = Category.find_or_create_by_category_name(product.category_name)
      product.merchant = Merchant.find_or_create_by_merchant_name(product.merchant_name)
      product.save!
    end
    

    It will take a while, so for large datasets you might need a better solution.

    So would this actually set the :category value in the products table to a category_id or set the value to the category_name?

    .find_or_create_by does a find on the attribute and returns the matching row, or creates one if it does not exist. When creating the association via `.category=, Rails will set the foreign key to match the id of the row in the categories table.

    So to answer your question more directly:

    Product.create(:category=>Category.find_or_create_by_name("Magic Beans"))
    

    is like doing this:

    category = Category.find_by_name("Magic Beans")
    if category.nil?
      category = Category.create(:name=>"Magic Beans")
    end
    product = Product.new
    product.category = category
    product.save
    

    where the penultimate step sets the foreign key category_id to the value category.id. By convention associations are set up such that the foreign key is the model name suffixed with _id, so your products table should have both category_id and merchant_id.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.