在创建记录时,我试图在创建父类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
但是,我正在部分地生成记录。 当@options生成时,对于给定的情况10结果(正确)。但是,只生成数量字段(通过上下文的quote_id rails句柄)。 option_id保持空白
Optionvolume id: 111, quote_id: 27, quantity: 0, created_at: [...]
Optionvolume id: 112, quote_id: 27, quantity: 0, created_at: [...]
等...
服务器甚至指示它应该处理数据
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]]
等...
答案 0 :(得分:0)
事实证明,服务器日志根本没有显示正在保存的option_id,但应用程序正在处理它。一个
<%= ff.object.inspect %>
命令显示它在那里。 http://guides.rubyonrails.org/debugging_rails_applications.html在这方面非常有帮助。