0
I have two classes Protocolo and Movimentacoes, in the rescue of Protocolo I create a new Movimentação.
class Protocolo < ActiveRecord::Base
has_many :movimentacoes
after_create :movimentacao_inicial
def movimentar(status, usuario, comentario = nil)
usuario = Usuario.find(usuario) if usuario.is_a?(Integer)
if movimentacoes.create(status: status, usuario: usuario, comentario: comentario).valid?
update_columns(status_atual: status)
else
false
end
end
def movimentacao_inicial
movimentar('enviado', usuario)
end
validates_associated :movimentacoes
end
E class Movement
class Movimentacao < ActiveRecord::Base
belongs_to :protocolo
validates :usuario, :protocolo, presence: true
end
So whenever Protocolo be saved, I must create a new Movimentação and update the current status of Protocolo with the status of the latter movimentação maid.
Problem: Case Movimentacao is invalid, Protocolo has already been created and I cannot create a movimentação before creating a protocolo.
Does anyone know any way around this?
Did you manage to analyze the answer Luiz? Can I try to help you?
– Breno Perucchi