I am preparing some seed data for a deployment from generated a CSV file. However, one of the model''s columns ( child_id) needs to have a null value in on a few of the rows where there is no Child instance. Whatever I have put in there, it seems to just come in as a zero. I have tried several combinations since I cannot find this documented anywhere. column1, child_id, column2 a,,b gives |a|0|b| a, null, b gives |a|0|b| a, "", b gives |a|0|b| a, nil, b gives |a|0|b Any ideas? Thanks. O. -- 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.
Owain wrote:> I am preparing some seed data for a deployment from generated a CSV > file. However, one of the model''s columns ( child_id) needs to have a > null value in on a few of the rows where there is no Child instance. > Whatever I have put in there, it seems to just come in as a zero. > > I have tried several combinations since I cannot find this documented > anywhere. > > column1, child_id, column2 > a,,b gives |a|0|b| > a, null, b gives |a|0|b| > a, "", b gives |a|0|b| > a, nil, b gives |a|0|bCheck your table schema to see if child_id has a default value configured. -- Posted via http://www.ruby-forum.com/. -- 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.
create_table "domains", :force => true do |t| t.string "column1" t.integer "child_id" t.string "column2" t.datetime "created_at" t.datetime "updated_at" end so there is nothing in there but a good suggestion to check first. Looks like 0 is the default value for integer and there is no way of specifying NULL. I can set up a workaround by adding another step in seeds to set all of the 0 values to NULL since I do not have any defined child_id''s of 0. I will have a look in the Fixtures code to see what is happening on this when I get the chance. O. -- 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.
Looks like the answer lies in CSV''s method each_with_index lib/active_record/fixtures.rb, line 720 def read_csv_fixture_files reader = CSV.parse(erb_render(IO.read(csv_file_path))) header = reader.shift i = 0 reader.each do |row| data = {} row.each_with_index { |cell, j| data[header[j].to_s.strip] cell.to_s.strip } self["#{@class_name.to_s.underscore}_#{i+=1}"] Fixture.new(data, model_class, @connection) end end I will do some testing on CSV directly against the file and see what I get once I have read the CSV documentation on null''s and nils. If that reads it ok then the problem lies somewhere in Fixture.new Anyone else with any better idea? -- 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.
Owain wrote:> Looks like the answer lies in CSV''s method each_with_index > > lib/active_record/fixtures.rb, line 720 > > def read_csv_fixture_files > reader = CSV.parse(erb_render(IO.read(csv_file_path))) > header = reader.shift > i = 0 > reader.each do |row| > data = {} > row.each_with_index { |cell, j| data[header[j].to_s.strip] > cell.to_s.strip } > self["#{@class_name.to_s.underscore}_#{i+=1}"] > Fixture.new(data, model_class, @connection) > end > end > > I will do some testing on CSV directly against the file and see what I > get once I have read the CSV documentation on null''s and nils. If > that reads it ok then the problem lies somewhere in Fixture.new > > Anyone else with any better idea?Perhaps try using YAML, which has a specific, unambiguous syntax for null values? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- 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.