如何在 Rails 中将一个模型的内容分配给另一个模型?
我有以下创建操作:
def create
@order = Order.new(params[:order])
if params[:same_as_above] == "1"
@order.billing_address.name = @order.shipping_address.name
@order.billing_address.number = @order.shipping_address.number
@order.billing_address.street = @order.shipping_address.town
end
if @order.save
if @order.purchase
render :action => "success"
else
render :action => "failure"
end
else
render :action => 'new'
end
end
它有效,但在我将送货地址逐个属性复制到帐单地址的方式中似乎有点麻烦和脆弱。请问有更好的办法吗?
I have the following create action:
def create
@order = Order.new(params[:order])
if params[:same_as_above] == "1"
@order.billing_address.name = @order.shipping_address.name
@order.billing_address.number = @order.shipping_address.number
@order.billing_address.street = @order.shipping_address.town
end
if @order.save
if @order.purchase
render :action => "success"
else
render :action => "failure"
end
else
render :action => 'new'
end
end
It works, but seems a bit cumbersome and brittle in the way i copy the shipping address to the billing address, attribute by attribute. Is there a better way please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就可以了。
does the trick.
如果您的模型设置正确,您可以使用:
If your models are set up correctly, you can use: