我正在尝试在irb上创建一个“借用”对象来测试我的数据库和表连接,但我不能。 如果我指定了:customer_id =>我已成功1创建命令。
数据库表中的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
My Cliente和Emprestimo模特:
class Emprestimo < ApplicationRecord
belongs_to :cliente
end
class Cliente < ApplicationRecord
has_one :endereco
has_many :emprestimos
end
非常感谢。
答案 0 :(得分:0)
实际上,在Rails 5中,默认情况下现在需要belongs_to
关系。它基本上会在你的foreing key中添加一个在线验证器。
您可以禁用此行为,将optional: true
添加为belongs_to
参数,如下所示:
belongs_to :cliente, optional: true
来自docs:
4.1.2.11:可选
如果将:optional选项设置为true,则不会验证相关对象的存在。默认情况下,此选项设置为false。