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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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_idWhy are you using lambda ? Fred> > :provider_id => lambda > { Provider.find_by_name(row[''Provider_Name'']) }, > > but didn''t succeed. Any help is appreciated. > > Thanks, > Harm-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 6, 6:00 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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> > :provider_id => lambda > > { Provider.find_by_name(row[''Provider_Name'']) }, > > > but didn''t succeed. Any help is appreciated. > > > Thanks, > > Harm-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 6, 9:12 pm, harm <176...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On Mar 6, 6:00 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> > 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.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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.