Ruby on Rails - Nested Attributes and Automatic object creation -
i asked previous question , 1 follows...
i have 3 models: contract, addendum , 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 addendum created, appointments created automatically. made work adding function on addendum's controller
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 called here
addendum controller def create respond_to |format| if @addendum.save format.html { redirect_to @addendum, notice: 'addendum created.' } format.json { render action: 'show', status: :created, location: @addendum } createnewappointments else ... end
now, because start using nested_attributes between contract , addendum, new addendum created using build
method, function createnewappointments
not called... how can this?
i tried using after_create createnewappointments
, on model addendum
doesn't work. gives error uninitialized constant contract::addendum
you have move method controller contracts model. models can't access controllers methods.
and replace @addendum self. phone call controller with:
@addendum.createnewappointments(params[:appointment], 'pendente')
note need pass params controller model create work.
def createnewappointments(appointment, descricao) appointment = appointment.new(params[:appointment]); appointment.dataprovavel = self.data appointment.addendum_id = self.addendum.id appointment.appointment_state = appointmentstate.where(descricao: descricao).first appointment.save ... end
your other alternative move method contract controller wich doing work right now. long addendums controller seems not interacting.
and like:
@addendum = contract.build.... create_new_appointments
hint: utilize rails conventions on naming methods. user lowercase underscores. don't java naming.
ruby-on-rails-4 nested-attributes
No comments:
Post a Comment