I asked a previous question and this one follows...
I have 3 Models: Contract, Addendum and Appointment
Contract.rb
has_many :addendums
accepts_nested_attributes_for :addendums
Addendum.rb
belongs_to :contract
has_many :appointments
Appointment.rb
belongs_to :addendum
When an Addendum is created, some Appointments are created automatically. I made this work adding a function on the Addendum's Controller like this
def createNewAppointments
appointment = Appointment.new(params[:appointment]);
appointment.dataprovavel = @addendum.data
appointment.addendum_id = @addendum.id
appointment.appointment_state = AppointmentState.where(descricao:'Pendente').first
appointment.save
...
end
This function is called here
Addendum Controller
def create
respond_to do |format|
if @addendum.save
format.html { redirect_to @addendum, notice: 'Addendum was successfully created.' }
format.json { render action: 'show', status: :created, location: @addendum }
createNewAppointments
else
...
end
Now, because I start using the nested_attributes between Contract and Addendum, the new Addendum is created using the build method, so the function createNewAppointments is not called... How can I do this?
I tried using after_create createNewAppointments, on the model Addendum but it doesn't work. It gives an error Uninitialized constant Contract::Addendum
Addendum Controllermeansclass AddendumsController?