Jay
2007-Jun-25 20:27 UTC
Importing flat CSV into two tables and maintaining a relationship
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2007-Jun-25 21:54 UTC
Re: Importing flat CSV into two tables and maintaining a relationship
On Jun 25, 2007, at 4:27 PM, Jay wrote:> 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, > > JayUse: 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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org