2017-02-11 137 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

请,张贴你给的命令,显示的错误(如果有的话)和你的'db/schema.rb'。 – mrlew

+0

@mrlew我发布了这些信息。谢谢。 –

+0

问题是数据没有被保存到数据库中?而已? – mrlew

回答

0

实际上,在rails 5中,belongs_to现在是默认关系。它基本上增加了一个存在验证器在你的前面的关键。

您可以禁用此行为将optional: truebelongs_to的说法,这样的:

belongs_to :cliente, optional: true 

docs

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