what is the proper syntax for seeding a child table?

I'm trying to seed a parent table and a child table. Seeding the parent works fine but I don't know the syntax for seeding the child. Please help.

  Contact.delete_all
    Customer.delete_all
    b=[]
   c=[]
    b[0]=[1231215,'Jeremy', 'G', '9177477337',
   'jt@gmail.com', 'Central Ave', 'Rockaway', 'NY', ' 12291', '76 Son Court',
   'ft lauderdale','Florida', '32423', '1', '0', '1', '1', '1', '1', '0', '0', '1',          '0', '9.95', 
  'Honda', '2012', 'Civic', '2.4 Turbo', 'Special', '1J474GFGDHDH8883334D0','fart monkey trap']

   b[1]=[46545465,'Se', 'Wof', '521428788',
   'steven.j.wolfman@gmail.com', '13 NE 17 Street', 'broward', 'FL', ' 32222', '13 NE 17 Street',
   'boca','Florida', '32222', '0', '0', '1', 
   '0', '0', '1', '1', 
   '1', '1', '1', '19.95', 
   'Ford', '2012', 'Taurus', '4.0', 'Faster', '3458GDHD3YU34D0','it smells']
   c[0]=[5112432709,'jg@gmail.com']
   c[1]=[4326546423,'sw@gmail.com']
   c[2]=[6614328902,'jt@gmail.com']


   i=0
   while i<2 do
    @customer=Customer.create(
        :uid=>b[i][0],
        :fname=>b[i][1],
        :lname=>b[i][2],
        :devphone=> b[i][3],
        :email=>b[i][4],
        :address=>b[i][5],
        :city=>b[i][6],
        :state=>b[i][7],
        :zip=>b[i][8],
        :baddress=>b[i][9],
        :bcity=>b[i][10],
        :bstate=>b[i][11],
        :bzip=>b[i][12],
        :roadass=>b[i][13],
        :crisisass=>b[i][14],
        :autocrash=>b[i][15],
        :emergencyass=>b[i][16],
        :remotediag=>b[i][17],
        :carfind=>b[i][18],
        :familytrack=>b[i][19],
        :lowbatt=>b[i][20],
        :towalerts=>b[i][21],
        :monthlycost=>b[i][22],
        :Make=>b[i][23],
        :Year=>b[i][24],
        :Model=>b[i][25],
        :Engine=>b[i][26],
        :VehicleSystem=>b[i][27],
        :vinnum=>b[i][28],
        :signupdate=>b[i][29],
        :password=>b[i][30],
        )
@customer.id=(1000000+i)
print "\n#{@customer.id}\n"

Up till this point the code works fine. When I add in the next 5 lines the code doesn't work.

      Contact.create(
      :customer_id=>@customer
      :contactmethod=>"sms",
      :contacttype=>c[i][0],
      :dateadded=>"5-1-2013",
      )
      i+=1
      end

This is the error I get when running db:seed: rake aborted! undefined method `Contact' for #

This is the Customer Base

      class Customer < ActiveRecord::Base
      attr_accessible :Engine, :Make, :Model, :VehicleSystem, :Year, :address, :autocrash, :baddress, :bcity, :bstate, :bzip, :carfind, :city, :crisisass, :devphone, :email, :emergencyass, :familytrack, :fname, :lname, :lowbatt, :monthlycost, :password, :remotediag, :roadass, :signupdate, :state,:stolenveh, :towalerts, :uid, :vinnum, :zip
      validates :email, :address,:fname, :lname, :password, :state, :uid, :zip, :presence => true
      has_many :Contacts
    end

This is the Contact Base

      class Contact < ActiveRecord::Base
        attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
        validates :contactmethod, :contacttype, :customer_id, :presence => true
        belongs_to :Customer
      end

Answer The answer to this question is that you can't require the presense of customer_id. Therefore, you just have to modify the validation by deleting :customer_id:

  class Contact < ActiveRecord::Base
    attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
    validates :contactmethod, :contacttype, :presence => true
    belongs_to :Customer
  end

Answers


Firstly don't name you variables with an @ symbol pre-pended, this is for instance variables in controllers and view, seed.rb is neither of those things.

Second of all, you have made the file very hard to understand, try simplifying it eg.

Contact.delete_all
Customer.delete_all
customer1 = Customer.create(
  # all of customer 1's details here
)
contact_for_customer_1 = Contact.create(
  customer_id: customer1.id,
  # customer 1's contact details
)
customer2 = Customer.create(
  # all of customer 2's details here
)
contact_for_customer_2 = Contact.create(
  customer_id: customer2.id,
  # customer 2's contact details
)

This will work and is easy to understand. You've already put all the customer details into the file right, so you may as well lay it out nicely. No need to store the details in an array and then build the customer's from there, just put lay it out more verbosely.


Need Your Help

iOS 8 Today Widget - SLComposeViewController not displaying properly

ios objective-c ios8 ios-app-extension today-extension

I've created an extension, in fact a Today Widget, using Xcode 6 and run it on my iOS 8 beta device (I am a registered iOS developer).

willRotateToInterfaceOrientation not working in Universal MasterDetail app

objective-c ios xcode ipad

So I have this code for changing the image on my UIImage view. And I get a very strange behavior both on iPad and iPhone.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.