# Importing flat CSV into two tables and maintaining a relatio

**URL:** <https://www.ruby-forum.com/t/importing-flat-csv-into-two-tables-and-maintaining-a-relatio/107746>\
**Category:** Rails\
**Created:** [June 25, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/importing-flat-csv-into-two-tables-and-maintaining-a-relatio/107746 "2007-06-25T22:27:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [June 25, 2007, 10:27pm UTC](https://www.ruby-forum.com/t/importing-flat-csv-into-two-tables-and-maintaining-a-relatio/107746/1 "2007-06-25T22:27:42Z")

</div>

Hello,

I’m new to Ruby on Rails and I’ve been really enjoying the framework.  
I have recently hit a wall that I can’t seem to get around. I’ve  
searched to web and haven’t found an answer(and my agile development  
with rails book hasn’t arrived)…

My problem: I have a CSV file that has information about recreation  
programs, and the buildings that each program is housed in. It is in  
a one line format.

So,

Building name | Location | Program name | Description of program |  
Time |

Buildings have many programs, but programs only have one building.

I need to import this CSV into a mysql database. I can do this using  
migrations and FasterCSV. There are duplicate entries for buildings,  
and I only want each building entered into the database once, which I  
could use validates uniqueness to ensure.

The wall I’ve encountered is how do I maintain the relationship  
between programs and buildings throughout this import process?

Ie I want to be able to list the programs in each building…

Many Thanks and apologies if there is a very obvious solution that I  
am missing,

Jay

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [June 25, 2007, 11:54pm UTC](https://www.ruby-forum.com/t/importing-flat-csv-into-two-tables-and-maintaining-a-relatio/107746/2 "2007-06-25T23:54:52Z")

</div>

On Jun 25, 2007, at 4:27 PM, Jay wrote:

> could use validates uniqueness to ensure.
> 
> The wall I’ve encountered is how do I maintain the relationship  
> between programs and buildings throughout this import process?
> 
> Ie I want to be able to list the programs in each building…
> 
> Many Thanks and apologies if there is a very obvious solution that I  
> am missing,
> 
> Jay

Use:

current\_building = Building.find\_or\_create\_by\_name(bldg\_name)

If you need to do something differently when the Building was created  
(like setup more attributes), you can:

# new\_record? is false on create, but nil on find

unless current\_building.new\_record?.nil?  
…  
end

Then I assume you have something like:

current\_building.programs.create(…)

or:

new\_program.building = current\_building

when you create the new program from the CSV data.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
