I have such a relationship in rails
class ReducaoZ < ActiveRecord::Base
self.table_name = 'reducaoz'
has_many :aliquotas, foreign_key: 'reducaoz_id', class_name: 'Aliquota', dependent: :delete_all
end
class Aliquota < ActiveRecord::Base
self.table_name = 'aliquota'
belongs_to :reducaoz, class_name: 'ReducaoZ'
end
And at a given moment, I instantiate several aliquots within the reduction
aliquota = reucao.aliquotas.build
aliquota.basecalculo = aliquota.valor
# outros valores
red.aliquotas << aliquota
and when I try to save the z reduction, the reducaoz_id
field is not there. as there is a restriction to not saving aliquot without reduction_id, activerecord throws an error.
apparently everything is correct, I can not see the error. Anyone have any idea what I missed?
Edit The sql that the rails tries to execute (along with the error) is this
SQL (23.4ms) INSERT INTO "aliquota" ("aliquota", "basecalculo", "valor") VALUES ($1, $2, $3) RETURNING "id" [["aliquota", "0300"], ["basecalculo", "0.0"], ["valor", "0.0"]]
PG::NotNullViolation: ERROR: null value in column "reducaoz_id" violates not-null constraint
: INSERT INTO "aliquota" ("aliquota", "basecalculo", "valor") VALUES ($1, $2, $3) RETURNING "id"
(1.0ms) ROLLBACK
ActiveRecord::StatementInvalid Exception: PG::NotNullViolation: ERROR: null value in column "reducaoz_id" violates not-null constraint