Ruby on rails 3.2 用于创建嵌套记录的rails3回调

Ruby on rails 3.2 用于创建嵌套记录的rails3回调,ruby-on-rails-3.2,callback,after-create,Ruby On Rails 3.2,Callback,After Create,在创建记录时,我试图在创建父类Quote时为相关类Optionvolume设置默认值 class Optionvolume < ActiveRecord::Base belongs_to :option belongs_to :quote class Option < ActiveRecord::Base has_and_belongs_to_many :tasks class Task < ActiveRecord::Base has_and_belongs

在创建记录时,我试图在创建父类Quote时为相关类Optionvolume设置默认值

class Optionvolume < ActiveRecord::Base
  belongs_to :option
  belongs_to :quote

class Option < ActiveRecord::Base
  has_and_belongs_to_many :tasks
class Task < ActiveRecord::Base
  has_and_belongs_to_many :options

class Quote < ActiveRecord::Base
  belongs_to :task
  has_many :optionvolumes, :dependent => :destroy
  has_many :options, through: :optionvolumes
  accepts_nested_attributes_for :optionvolumes, :allow_destroy => true    

  after_create :set_optionvolumes

  private
    def set_optionvolumes
      @options = Option.where(['options_tasks.task_id = ?', self.task_id]).joins(:tasks)
      @options.each do |option|
        self.optionvolumes.create(:quantity => '0', :option_id => option.id)
      end
    end
等等……
服务器甚至指示它应该处理数据

INSERT INTO "optionvolumes" ("option_id", "quote_id", "created_at", "quantity", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["option_id", 15], ["quote_id", 38], ["created_at", Tue, 13 Aug 2013 11:20:07 UTC +00:00], ["quantity", 0], ["updated_at", Tue, 13 Aug 2013 11:20:07 UTC +00:00]]
INSERT INTO "optionvolumes" ("option_id", "quote_id", "created_at", "quantity", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["option_id", 16], ["quote_id", 38], ["created_at", Tue, 13 Aug 2013 11:20:07 UTC +00:00], ["quantity", 0], ["updated_at", Tue, 13 Aug 2013 11:20:07 UTC +00:00]]

等等。

事实证明,服务器日志并没有显示正在保存的选项id,但应用程序正在处理它。A

<%= ff.object.inspect %>

命令显示它在那里。在这方面很有帮助

<%= ff.object.inspect %>