在我的子分类帐会计轨道应用程序中,我有一个资金模型
class Fund < ActiveRecord::Base
belongs_to :agency
has_many :gl_accounts
accepts_nested_attributes_for :gl_accounts
attr_accessible :name, :agency_id, :fund, :user_stamp, :active
attr_accessible :gl_accounts_attributes
和一个 gl_accounts 模型
class GlAccount < ActiveRecord::Base
belongs_to :agency
belongs_to :fund
has_many :class_sessions
has_many :facilities
validates :agency_id, :fund_id, :name, :gl_account_number, :active, :user_stamp, :account_type, :presence => true
validates_uniqueness_of :account_type, :scope => :fund_id, :if => :unique_account_type
attr_accessible :agency_id, :fund_id, :name, :gl_account_number, :active, :user_stamp, :account_type
def unique_account_type
[3,4,6,7,8].include? account_type
end
创建新基金时,必须同时创建 5 个 gl_accounts,因此我fields_for
在为基金创建新记录时在 gl_account 模型中创建 5 个新记录。一切似乎都正常,直到我提交表格并且我收到一条错误消息,提示“Gl 账户资金不能为空白”。
gl_accounts 模型上没有“fund”属性。我认为也许rails正在删除“_id”部分(因为有一个fund_id外键字段)但我理解使用嵌套模型并fields_for
自动在fund_id字段中添加正确的值(gl_account模型的外键)。但是,即使我在表单中添加了一个带有 fund_id 值的隐藏字段,我仍然会收到错误消息,指出“fund”不能为空。
所以,也许 Rails 试图告诉我我还有其他问题?
以下是参数:
{"utf8"=>"✓",
"authenticity_token"=>"MNWLFOnLOE+ZRsUf9mogf2cq/TeQ+mxtrdaVu3bEgpc=",
"fund"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"name"=>"Junk",
"fund"=>"44",
"active"=>"1",
"gl_accounts_attributes"=>{"0"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"account_type"=>"6",
"name"=>"Cash Account",
"active"=>"1",
"fund_id"=>"1",
"gl_account_number"=>"44-498-965-789"},
"1"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"account_type"=>"7",
"name"=>"Credit Card Account",
"active"=>"1",
"fund_id"=>"1",
"gl_account_number"=>"44-498-965-163"},
"2"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"account_type"=>"3",
"name"=>"Customer Account Balances",
"active"=>"1",
"fund_id"=>"1",
"gl_account_number"=>"44-498-965-254"},
"3"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"account_type"=>"8",
"name"=>"Refunds Pending Account",
"active"=>"1",
"fund_id"=>"1",
"gl_account_number"=>"44-498-965-456"},
"4"=>{"agency_id"=>"1",
"user_stamp"=>"6",
"account_type"=>"4",
"name"=>"Deferred Revenue Account",
"active"=>"1",
"fund_id"=>"1",
"gl_account_number"=>"44-498-965-159"}}},
"commit"=>"Add New Fund"}