Rails使用一对一关联创建方法验证错误

时间:2016-07-28 16:32:07

标签: ruby-on-rails ruby activerecord

我一直在Coursera学习ruby,特别是在一对一的协会模块中。我试图在我自己的系统上模拟教程中的一对一关联示例,但是我得到了ActiveRecord :: RecordInvalid错误。这就是我为达到这一点所做的工作:

1)rails g model person first_name last_name 2)rake db:migrate 3)rails g model personal_info height:float weight:float person references 4)rake db:migrate 5)迁移20160725143537_create_people.rb具有以下代码:

class CreatePeople < ActiveRecord::Migration[5.0]
 def change
   create_table :people do |t|
    t.string :first_name
    t.integer :age
    t.string :last_name
    t.timestamps
  end
 end
end

6)迁移20160727184247_create_personal_infos.rb具有以下代码: -

class CreatePersonalInfos < ActiveRecord::Migration[5.0]
  def change
    create_table :personal_infos do |t|
      t.float :height
      t.float :weight
      t.references :person, foreign_key: true

      t.timestamps null: false
   end
end

7)person.rb中的代码如下:

class Person < ApplicationRecord
  has_one :personal_info
end

8)personal_info.rb中的代码如下: -

class PersonalInfo < ApplicationRecord
  belongs_to :person
end

9)seeds.rb中的代码如下: -

Person.destroy_all

Person.create! [
{first_name: "Kalman", last_name: "Smith", age: 33, login: "kman", pass: "abc123"},
{first_name: "John", last_name: "Whatever", age: 27, login: "john1", pass: "123abc"},
{first_name: "Michael", last_name: "Smith", age: 15, login: "mike", pass: "not_telling"},
{first_name: "Josh", last_name: "Oreck", age: 57, login: "josh", pass: "password1"},
{first_name: "John", last_name: "Smith", age: 27, login: "john2", pass: "no_idea"},
{first_name: "Bill", last_name: "Gates", age: 75, login: "bill", pass: "windows3.1"},
{first_name: "LeBron", last_name: "James", age: 30, login: "bron", pass: "need more rings"},]

10)现在我转到rails控制台(rails c)并键入以下内容: pi1 = PersonalInfo.create!(身高:6.2,体重:220.0)。这是当我得到上述错误,如下所示:

IRB(主):002:0&GT; pi1 = PersonalInfo.create!(身高:6.2,体重:220.0)

(0.0ms)  begin transaction
(0.0ms)  rollback transaction
    ActiveRecord::RecordInvalid: Validation failed: Person must exist
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/validations.rb:78:in `raise_validation_error'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/validations.rb:50:in `save!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:324:in `block in save!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:395:in `block in with_transaction_returning_status'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:392:in `with_transaction_returning_status'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:324:in `save!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/suppressor.rb:45:in `save!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/persistence.rb:51:in `create!'
    from (irb):2
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/console.rb:65:in `start'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in `start'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:in `console'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'irb(main):003:0>

我正在使用rails 5。 我真的很感谢你们帮助我解决这个错误。提前致谢

3 个答案:

答案 0 :(得分:1)

根据您的要求,您已在has_onePerson之间定义了PersonalInfo关联。 首先创建PersonalInfo的最佳方法是创建Person然后PersonalInfo,例如:

@person = Person.create!(first_name: "Kalman", last_name: "Smith", age: 33, login: "kman", pass: "abc123")

现在您可以创建PersonalInfo

@person.personal_info.create(height:6.2, weight:220.0)

我认为这会对你有所帮助。如果您有任何疑虑,请告诉我。

答案 1 :(得分:0)

您需要设置$(document).click(function () { $('#box2').hide(); }); $('#box1').click(function (e) { e.stopPropagation(); $('#box2').show(); });

person_id

答案 2 :(得分:0)

正如您在CreatePersonalInfos迁移中所看到的,person引用具有外键约束。这意味着您无法在没有关联PersonalInfo的情况下创建Person。您可以通过提供有效的person_id

来创建它
PersonalInfo.create!(height:6.2, weight:220.0, person_id:1)

或通过人员协会

@person.create_personal_info(height:6.2, weight:220.0)

您还可以在迁移中删除foreign_key选项以删除数据库级约束。