# Fastercsv and associated records

**URL:** <https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684>\
**Category:** Rails\
**Created:** [March 6, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684 "2010-03-06T18:01:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 6, 2010, 6:01pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684/1 "2010-03-06T18:01:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 6, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684/2 "2010-03-06T18:02:03Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 6, 2010, 10:13pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684/3 "2010-03-06T22:13:44Z")

</div>

On Mar 6, 6:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 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

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 7, 2010, 6:16pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684/4 "2010-03-07T18:16:29Z")

</div>

On Mar 6, 9:12 pm, harm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 6, 6:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

> ```
> )
> 
> ```
> 
> the column just does not get filled.

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

---

<div class="post-metadata">

**Author:** ![harm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b4bc9f/32.png) [@harm](https://www.ruby-forum.com/u/harm)\
**Post date:** [March 7, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/fastercsv-and-associated-records/184684/5 "2010-03-07T21:40:14Z")

</div>

> 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.
