0

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
stevo999999
  • 588
  • 4
  • 12

2 Answers2

2

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.

Norto23
  • 2,249
  • 3
  • 23
  • 40
  • essentially what I get from what you are showing me is to put in the line 'customer_id: customer1.id,' and then you don't like how my code looks so you want to make it pretty. However, your solution doesn't work. I still get exactly the same error. – stevo999999 May 02 '13 at 02:14
  • Also in your models, your belongs_to calls shouldn't have capitals for the model names, ie `belongs_to :Customer` becomes `belongs_to :customer`. This is a real problem. Your associations won't work like this, therefore it could be the source of your problem – Norto23 May 02 '13 at 03:42
  • Also it's not about making it pretty, it's about making it readable. I mean your style is terrible which leads to problems like `undefined method Contact for #` – Norto23 May 02 '13 at 03:46
  • I guess what I'm trying to say, is that if you open a question asking the proper syntax for a seeds file, and it's given to you...don't be a smart ass – Norto23 May 02 '13 at 03:50
  • I got the code working but just for arguments sake I tried changing belongs_to :Customer to belongs_to :customer and then the code stopped working again. The reason I was upset was not because you harped on the my syntax but rather because you didn't fix the problem and instead you put me on a while goose chase of playing with the syntax. – stevo999999 May 02 '13 at 19:18
0

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, :customer_id, :presence => true
    belongs_to :Customer
  end
stevo999999
  • 588
  • 4
  • 12