播种子表的正确语法是什么?

时间:2013-05-02 01:18:43

标签: ruby-on-rails ruby database belongs-to seeding

我正在尝试播种父表和子表。播种父母工作正常,但我不知道播种孩子的语法。请帮忙。

  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"

到目前为止,代码工作正常。当我在接下来的5行中添加代码时,代码不起作用。

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

这是我在运行db:seed时遇到的错误: 耙子流产了!对于#

,未定义的方法“联系”

这是客户群

      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

这是联络基地

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

答案 这个问题的答案是你不能要求提供customer_id。因此,您只需通过删除:customer_id:

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

2 个答案:

答案 0 :(得分:2)

首先不要用预先设置@符号的变量命名变量,例如控制器和视图中的变量,seed.rb既不是那些东西。

其次,你使文件很难理解,试着简化它,例如。

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
)

这将有效且易于理解。您已将所有客户详细信息都放入文件中,因此您可以很好地将其详细说明。无需将详细信息存储在一个数组中,然后从那里构建客户,只需更详细地说明。

答案 1 :(得分:0)

这个问题的答案是您不能要求客户ID存在。因此,您只需通过删除:customer id:

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