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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:46:00+00:00 2026-06-18T09:46:00+00:00

I am following the Import CSV Railscast and it is straight forward. The issue

  • 0

I am following the Import CSV Railscast and it is straight forward.

The issue is that it deals with just a csv file containing only information in 1 model in 1 file.

Say, I have a CSV file that I am trying to import to my Listing model. On each row/listing it has a column called Building where the value is actually the name of the building attribute of that listing (i.e. @listing.building.name).

How do I handle those cases in the import?

This is the closet that Ryan gets in that Railscast, which is on the Product model in his case:

def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product.save!
  end
end

All that is happening is that he is checking to see if the product exists and if it does then update the attributes. If it doesn’t, then create a new one.

Not quite sure how to handle the associations in this case…especially given that what needs to happen is in the event that an associated record doesn’t exist, it needs to be created during this process.

So going back to my building.name example earlier, if there is no Building.find_by_name(name), then it should create a new building record.

Thoughts?

  • 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-18T09:46:01+00:00Added an answer on June 18, 2026 at 9:46 am

    try this

    def self.import(file)
      CSV.foreach(file.path, headers: true) do |row|
        product = find_by_id(row["id"]) || new
        product.attributes = row.to_hash.slice(*accessible_attributes)
        product.save!
    
        building = product.buildings.find_by_name(row['building_name'])
        building ||= product.buildings.build
        building.attributes = row.to_hash.slice(*build_accessible_attributes)
        building.save!
      end
    end
    

    UPDATE: updated answers using new rails 3 methods

    def self.import(file)
      CSV.foreach(file.path, headers: true) do |row|
        product = where(id: row["id"])
          .first_or_create!(row.to_hash.slice(*accessible_attributes))
    
        product.buildings.where(name: row['building_name'])
          .first_or_create!(row.to_hash.slice(*building_accessible_attributes))
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am following the Import CSV Railscast and it is straight forward. I added
I have the following admin action below that exports data to a CSV file,
I have the following scenario: I have to import a CSV file to a
I'm trying to import data from a csv file following the example given by
Im using the following code to import a csv that has two columns -
I got the following code (working): #Import File $Users = Import-Csv C:\Users\Administrator\Desktop\userImport\users.csv # Setting
I tried to import csv file data into postgres table. Running the following line
I am reading in a .csv file similar to the following. Note that PhoneNumbers
I have the following code that is part of a tutorial import csv as
I'm using following function to import the csv file into db. All is working

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.