0
customer=Customer.new #create a new Customer object
customer.id =1000+i #Id needs to be set first because otherwise its automatically set
customer.update_attributes(
    :fname=>'mike',
    :lname=>'hancock',
    :Year=>1998,
    :Model=>'buick',
    :Engine=>'4 liter',
    :vinnum=>'h920129801298',
    :signupdate=>"#{Date.today}",
    :password=>'fguygfyed',
)
    contact=Contact.create(
        :customer_id=>customer.id, #set the foreign primary key
        :contactmethod=>4567894561,
        :contacttype=>"sms",
        :dateadded=>"#{Date.today}",
    )
    customer.Contacts.create(
        :contactmethod=> 4657894564,
        :contacttype=> 'email',
        :dateadded=> "#{Date.today}",
    )
    i+=1
end

此代码有效。但是,如果不是

contact=Contact.create( :customer_id=>customer.id, #设置外键

我只是像最后写的那样写了 customer.Contacts.create 然后代码失败了。为什么我只能有一个 customer.Contacts.create 实例,而另一个我必须明确声明 customer.id?

4

1 回答 1

0

方法名称几乎总是声明为全小写。这似乎无效。

您可能想要的是:

customer.contacts.create(...)
于 2013-05-02T19:17:45.730 回答