Ruby on rails 种子设定子表的正确语法是什么?

Ruby on rails 种子设定子表的正确语法是什么?,ruby-on-rails,ruby,database,belongs-to,seeding,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 lauder

我正在尝试为父表和子表设置种子。为父对象设定种子效果很好,但我不知道为子对象设定种子的语法。请帮忙

  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"
这是我在运行db:seed时遇到的错误: 雷克流产了!未定义的方法“Contact”#

这是客户群

      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客户true
你有很多联系人吗
结束
这是联络基地

      class Contact < ActiveRecord::Base
        attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
        validates :contactmethod, :contacttype, :customer_id, :presence => true
        belongs_to :Customer
      end
class联系人true
属于:客户
结束
答复 这个问题的答案是,您不能要求提供customer\u id。因此,您只需通过删除:customer\u id:

  class Contact < ActiveRecord::Base
    attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
    validates :contactmethod, :contacttype, :presence => true
    belongs_to :Customer
  end
  class Contact < ActiveRecord::Base
    attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
    validates :contactmethod, :contacttype, :customer_id, :presence => true
    belongs_to :Customer
  end
class联系人true
属于:客户
结束

首先,不要在命名变量时预先使用
@
符号,例如控制器和视图中的变量,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
)

这将起作用,并且易于理解。您已经将所有客户详细信息正确地放入了该文件中,因此您可以很好地进行布局。无需将详细信息存储在阵列中,然后从阵列中构建客户的,只需更详细地布局即可。

这个问题的答案是,您不需要提供客户id。因此,您只需通过删除:customer\u id:

  class Contact < ActiveRecord::Base
    attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
    validates :contactmethod, :contacttype, :presence => true
    belongs_to :Customer
  end
  class Contact < ActiveRecord::Base
    attr_accessible :contactmethod, :contacttype, :customer_id, :dateadded
    validates :contactmethod, :contacttype, :customer_id, :presence => true
    belongs_to :Customer
  end
class联系人true
属于:客户
结束

从您向我展示的内容中,我得到的实质是在“customer\u id:customer1.id”行中添加一行,然后您不喜欢我的代码的外观,因此希望使其美观。但是,您的解决方案不起作用。我仍然得到完全相同的错误。此外,在您的模型中,您的“属于”呼叫不应在模型名称中使用大写字母,即
属于\u to:Customer
变成
属于\u to:Customer
。这是一个真正的问题。你的联想不会像这样工作,因此它可能是你问题的根源。它不是为了让它好看,而是为了让它可读。我的意思是,你的风格很糟糕,这会导致一些问题,比如#未定义的方法联系人我想我想说的是,如果你打开一个问题,询问种子文件的正确语法,这是给你的…别傻了,我让代码工作了,但只是为了参数的缘故,我试着把bells\u to:Customer改成bells\u to:Customer,然后代码又停止工作了。我不高兴的原因不是因为你反复强调我的语法,而是因为你没有解决问题,而是你让我玩了一会儿语法。customer\u id应该收到
@customer.id
,而不仅仅是
@customer