轨道4创建相关的对象上的保存 [英] Rails 4 Create Associated Object on Save

查看:232
本文介绍了轨道4创建相关的对象上的保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能自动创建多个关联的对象就在我保存一个新的主对象?

例如

在轨道4,我有三个对象:企业 预算类别

 #应用程序/模型/ business.rb
一流的商业与LT; ActiveRecord的::基地
   #attrs ID,名称
   的has_many:预算
结束#应用程序/模型/ budget.rb
一流的预算和LT; ActiveRecord的::基地
   #attrs ID,business_id,部门标识,值
   belongs_to的:业务
   belongs_to的:类别
结束#应用程序/模型/ category.rb
类分类和LT; ActiveRecord的::基地
   #attrs ID,名称
   的has_many:预算
结束

当我创建一个新的业务,节省了新的业务后,我想以原子为每个类别一份财政预算案,并给它$ 0值。这样一来,当我去显示或编辑一个新的业务,它已经具有相关的类别和预算,然后可以进行编辑。因此,在创建新的业务,多种新的预算将被创建,每个类别,各有值为0。

我读这篇文章:<一href=\"http://stackoverflow.com/questions/3739158/rails-3-how-add-a-associated-record-after-creating-a-primary-record-books-aut\">Rails 3,如何创建主记录后,添加相关的记录(书籍,自动添加BookCharacter)

和我想知道如果我要在业务模型中使用after_create回调,并有逻辑,那么在预算控制存在(不完全知道如何做到这一点),或者我是否应该添加逻辑来在该businesses_controller.rb 新有类似的东西叫:

  @business = Business.new
@categories = Category.all
@ categories.each做|类别|
      category.budget.build(:值=&gt;中0时,:business_id = GT; @ business.id)
结束


解决方案

我结束了在所有类别中添加逻辑,以在业务控制create方法循环并创建一个预算只保存了。请注意,我是偷懒,没有把任何错误处理。

  DEF创建
    @business = Business.new(PARAMS [:业务])    @Results = @ business.save    @categories = Categories.all    @ categories.each做|类别|
      category.budgets.create(:量=&gt;中0时,:business_id = GT; @ business.id)
    结束
    做的respond_to |格式|
      ...
    结束
  结束

How can I create multiple associated objects automatically just after I save a new primary object?

For example

In Rails 4, I have three objects: Businesses, Budgets, and Categories.

#app/models/business.rb
class Business < ActiveRecord::Base
   #attrs id, name
   has_many :budgets
end

#app/models/budget.rb
class Budget < ActiveRecord::Base
   #attrs id, business_id, department_id, value
   belongs_to :business 
   belongs_to :category
end

#app/models/category.rb
class Category < ActiveRecord::Base
   #attrs id, name
   has_many :budgets
end

When I create a new Business, after saving the new Business, I would like to atomically create a Budget for each Category and give it value of $0. This way, when I go to show or edit a new Business, it will already have the associated Categories and Budgets, which can then be edited. Thus, upon creating a new Business, multiple new Budgets will be created, one for each Category, each with the value of 0.

I read this article: Rails 3, how add a associated record after creating a primary record (Books, Auto Add BookCharacter)

And I am wondering if I should use the after_create callback in the Business model and have the logic then exist in the Budgets controller (not exactly sure how to do this) or if I should add logic to the businesses_controller.rb in the 'new' call with something similar to:

@business = Business.new
@categories = Category.all
@categories.each do |category|
      category.budget.build(:value => "0", :business_id => @business.id)
end

解决方案

I ended up adding the logic to the create method in the Business controller to loop through all Categories and create a budget just after save. Note that I was lazy and didn't put in any error handling. :

  def create
    @business = Business.new(params[:business])

    @results = @business.save

    @categories = Categories.all

    @categories.each do |category|
      category.budgets.create(:amount => "0", :business_id => @business.id)
    end


    respond_to do |format|
      ...
    end
  end

这篇关于轨道4创建相关的对象上的保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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