fastercsv and associated records

Hi, I try to import data with a rake-task from a csv-file into a model like:

provider, which has_many :rates

and

rate, which belong_to: provider

I do the following:

namespace :db do   desc "load data from csv"   task :load_csv_data => :environment do     require 'fastercsv'

    FasterCSV.foreach("importdata/tarifs.csv",       :headers => true, :col_sep => ',') do |row|

      Provider.find_or_create_by_name(         :name => row['Provider_Name'],         :hotline => row['Hotline'],         :email => row['Email']         )

      Rate.find_or_create_by_name(         :provider => lambda {                      Provider.find_by_name(row['Provider_Name']) },         :name => row['Rate_Name'],         :preis => row['Preis']         )     end   end end

But the association-key (provider_id) in the table "rates" won't be writen to the database! I also tried accessing directly provider_id

:provider_id => lambda { Provider.find_by_name(row['Provider_Name']) },

but didn't succeed. Any help is appreciated.

Thanks, Harm

  Rate\.find\_or\_create\_by\_name\(
    :provider => lambda \{
                 Provider\.find\_by\_name\(row\['Provider\_Name'\]\) \},
    :name => row\['Rate\_Name'\],
    :preis => row\['Preis'\]
    \)
end

end end

But the association-key (provider_id) in the table "rates" won't be writen to the database! I also tried accessing directly provider_id

Why are you using lambda ?

Fred

> Rate.find_or_create_by_name( > :provider => lambda { > Provider.find_by_name(row['Provider_Name']) }, > :name => row['Rate_Name'], > :preis => row['Preis'] > ) > end > end > end

> But the association-key (provider_id) in the table "rates" won't be > writen to the database! I also tried accessing directly provider_id

Why are you using lambda ?

it came from an earlier solution

Actually I need to fill the column "provider_id" in the table "rates" with the associated key-id from the table "provider"!

When I do something like:

      Rate.find_or_create_by_name(         :provider_id => Provider.find_by_name(row['Provider_Name']),         :name => row['Rate_Name'],         :preis => row['Preis']         )

the column just does not get filled.

Harm

When filling an id column like that you should probably be passing an id rather than a Provider object (although :provider => Provider.find_by_name(...) should be ok). Sounds like you should break this down into smaller steps so that you see where it is going wrong (eg so that you can check whether a provider is being found at all)

Fred

When filling an id column like that you should probably be passing an id rather than a Provider object (although :provider => Provider.find_by_name(...) should be ok). Sounds like you should break this down into smaller steps so that you see where it is going wrong (eg so that you can check whether a provider is being found at all)

I also thought that and finaly did the trick! Now I do something like:

    FasterCSV.foreach("importdata/tarife.csv", :headers => true, :col_sep => ',') do |row|       Anbieter.find_or_create_by_name( :name =>row['Anbieter_Name'] )

      associated_anbieter = Anbieter.find_by_name(row['Anbieter_Name'])       associated_kategorie = Kategorie.find_by_name(row['Kategorie'])       associated_netz = Netz.find_by_name(row['Netz'])

      Tarif.create(         :anbieter_id => associated_anbieter.id,         :kategorie_id => associated_kategorie.id,         :netz_id => associated_netz.id,         :name => row['Tarif_Name'] )     end

Probably not really a beauty but working. Anyway thanks for your feedback.