2017-02-11 5 views
-2

私はデータベースとテーブルの接続をテストするためにirbに "貸出"オブジェクトを作成しようとしていますが、できません。 作成コマンドで:customer_id => 1を指定すると正常に終了しました。IRBにオブジェクトを作成する

データベーステーブルのcustomer_idフィールドがNOTNULLとして解決されていません。

誰でもお手伝いできますか?

これは私がしようとしているコマンドとエラーになります:

irb(main):004:0> emprestimo = Emprestimo.create(:valor => 10000.00, :qnt_parcelas => 10, :valor_parcelas => 1000.00, :banco => 'Bic', :corretora => 'milreais') 
    (0.2ms) BEGIN 
    (0.2ms) ROLLBACK 
=> #<Emprestimo id: nil, cliente_id: nil, valor: 10000.0, qnt_parcelas: 10, valor_parcelas: 1000.0, data_emprestimo: nil, banco: "Bic", corretora: "milreais", created_at: nil, updated_at: nil> 

マイ/db.schema.rb:

ActiveRecord::Schema.define(version: 20170208154641) do 

    create_table "clientes", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 
    t.string "nome",  limit: 45, null: false 
    t.string "cpf",  limit: 14, null: false 
    t.string "rg",   limit: 15, null: false 
    t.string "matricula", limit: 20, null: false 
    t.string "senha",  limit: 10 
    t.date  "data_nasc" 
    t.string "orgao",  limit: 30 
    t.string "tel",  limit: 15, null: false 
    t.string "tel2",  limit: 15 
    t.string "convenio", limit: 10, null: false 
    t.string "email",  limit: 35 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    end 

    create_table "emprestimos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 
    t.integer "cliente_id" 
    t.float "valor",   limit: 24, null: false 
    t.integer "qnt_parcelas", limit: 3, null: false 
    t.float "valor_parcelas", limit: 24, null: false 
    t.date  "data_emprestimo" 
    t.string "banco",   limit: 40, null: false 
    t.string "corretora",  limit: 40 
    t.datetime "created_at",     null: false 
    t.datetime "updated_at",     null: false 
    end 

    create_table "enderecos", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 
    t.integer "cliente_id" 
    t.string "rua",   limit: 45, null: false 
    t.bigint "numero",     null: false 
    t.string "complemento", limit: 45, null: false 
    t.string "bairro",  limit: 45, null: false 
    t.string "cidade",  limit: 45, null: false 
    t.string "estado",  limit: 2, null: false 
    t.string "cep",   limit: 9, null: false 
    t.datetime "created_at",    null: false 
    t.datetime "updated_at",    null: false 
    end 

    create_table "operadors", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| 
    t.string "user",  limit: 45 
    t.string "senha",  limit: 6 
    t.datetime "created_at",   null: false 
    t.datetime "updated_at",   null: false 
    end 

end 

マイClienteとEmprestimoモデル:

class Emprestimo < ApplicationRecord 

    belongs_to :cliente 
end 

class Cliente < ApplicationRecord 
    has_one :endereco 
    has_many :emprestimos 
end 

ありがとうございます。

+1

てください:

belongs_to :cliente, optional: true 

docsからあなたの 'db/schema.rb'。 – mrlew

+0

@mrlewこれらの情報を掲載しました。ありがとうございました。 –

+0

問題は、データがデータベースに永続化されていないことです。それでおしまい? – mrlew

答えて

0

実際、Rails 5ではデフォルトでbelongs_toの関係が必要です。基本的に、あなたのforeingキーにプレゼンスバリデータを追加します。

あなたはこのように、optional: truebelongs_toとして引数を追加し、この動作を無効にすることができます:エラーが(もしあれば)に示す、あなたが与えたコマンドを投稿し、

4.1.2.11 :optional

If you set the :optional option to true, then the presence of the associated object won't be validated. By default, this option is set to false.

+0

@mrlewありがとうございました。 :) –

+0

よろしくお願いします! – mrlew

関連する問題