Ich habe Ruby auf Schienen von Coursera gelernt und bin speziell im Eins-zu-eins-Assoziationsmodul. Ich habe versucht, das Eins-zu-Eins-Assoziationsbeispiel im Tutorial auf meinem eigenen System zu emulieren, aber ich erhalte den ActiveRecord :: RecordInvalid-Fehler. Dies ist, was ich habe diesen Punkt zu erreichen getan:Rails Create Methode Validation Fehler mit eins zu eins Assoziation
1) Schienen g Modell Person first_name 2) rake db last_name: wandern 3) Schienen g Modell personal_info Höhe: float Gewicht: float Person verweist 4) rake db : migrieren 5) Migration 20160725143537_create_people.rb hat den folgenden Code:
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) Migration 20160727184247_create_personal_infos.rb den folgenden Code hat: -
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
Ende
7) Der Code in person.rb ist wie folgt:
class Person < ApplicationRecord
has_one :personal_info
end
8) Der Code in personal_info.rb ist wie folgt: -
class PersonalInfo < ApplicationRecord
belongs_to :person
end
9) Der Code in seeds.rb ist wie folgt: -
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) Jetzt gehe ich auf die Schienen Konsole (Schienen c) und geben Sie den folgenden : pi1 = PersonalInfo.create! (Höhe: 6.2, Gewicht: 220.0). Dies ist, wenn ich erhalte den oben genannten Fehler, der wie folgt aussieht:
irb (main): 002: 0> PI1 = PersonalInfo.create! (Höhe: 6.2, Gewicht: 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>
Ich bin mit Schienen 5. Ich würde wirklich schätzen, dass Sie mir helfen, diesen Fehler zu beheben. Vielen Dank im Voraus
Vielen Dank für Ihre Hilfe! –