【问题标题】:what is the proper syntax for seeding a child table?播种子表的正确语法是什么?
【发布时间】:2013-05-02 01:18:43
【问题描述】:

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

  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 时遇到的错误: 耙中止! #

的未定义方法 `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 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

【问题讨论】:

  • customer_id 应该收到@customer.id,而不仅仅是@customer

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


【解决方案1】:

首先不要用@ 符号为变量命名,这是控制器和视图中的实例变量,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
)

这将起作用并且易于理解。您已经将所有客户详细信息正确地放入了文件中,因此您不妨好好布置一下。无需将详细信息存储在数组中,然后从那里构建客户的,只需更详细地布局即可。

【讨论】:

  • 基本上我从你向我展示的内容中得到的是放入'customer_id:customer1.id'行,然后你不喜欢我的代码看起来所以你想让它漂亮.但是,您的解决方案不起作用。我仍然得到完全相同的错误。
  • 同样在你的模型中,你的belongs_to调用不应该有模型名称的大写字母,即belongs_to :Customer变成belongs_to :customer。这是一个真正的问题。您的关联不会像这样工作,因此它可能是您的问题的根源
  • 另外,它不是为了让它漂亮,而是为了让它可读。我的意思是你的风格很糟糕,这会导致像undefined method Contact for #这样的问题
  • 我想我想说的是,如果您打开一个问题,询问种子文件的正确语法,并且它已提供给您...不要做一个聪明的驴子
  • 我让代码正常工作,但只是为了争论,我尝试将 belongs_to :Customer 更改为 belongs_to :customer 然后代码再次停止工作。我不高兴的原因不是因为你在我的语法上喋喋不休,而是因为你没有解决问题,而是让我在玩语法的时候玩了一会儿鹅。
【解决方案2】:

这个问题的答案是您不能要求存在 customer_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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2014-04-19
    • 2018-11-14
    • 2019-09-07
    相关资源
    最近更新 更多