Ruby on Rails-嵌套属性和自动对象创建 [英] Ruby on Rails - Nested Attributes and Automatic object creation

查看:103
本文介绍了Ruby on Rails-嵌套属性和自动对象创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了以前的

I asked a previous question and this one follows...

我有3种模式:合同,附录和任命

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

创建附录后,会自动创建一些约会.我做了这样的工作,像这样在Addendum's Controller上添加了一个功能

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

此功能在这里称为

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

现在,由于我开始使用Contract和Addendum之间的nested_attributes,因此使用build方法创建了新的Addendum,因此未调用函数createNewAppointments ...该怎么办?

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?

我尝试在模型Addendum上使用after_create createNewAppointments,但是它不起作用.它给出了错误Uninitialized constant Contract::Addendum

I tried using after_create createNewAppointments, on the model Addendum but it doesn't work. It gives an error Uninitialized constant Contract::Addendum

推荐答案

,您将不得不将方法从控制器移至合同模型.模型无法访问控制器方法.

you will have to move the method from the controller to the contracts model. Models can't access controllers methods.

并将@addendum替换为self.然后使用以下命令从控制器调用它:

And replace @addendum with self. Then call it from the controller with:

@addendum.createNewAppointments(params[:appointment], 'pendente')

请注意,您需要将参数从控制器传递到模型以使其起作用.

Note that you need to pass the params from the controller to the model to make it 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 option is to move the method to the contract controller wich is doing the work right now. As long as the addendums controller seems not to be interacting.

并执行类似的操作:

@addendum = contract.build....
create_new_appointments

提示:在命名方法上使用rails约定.用户小写的下划线.不要进行JAVA命名.

HINT: Use rails conventions on naming methods. User lowercase underscores. Don't do JAVA naming.

这篇关于Ruby on Rails-嵌套属性和自动对象创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆