2

在我的子分类帐会计轨道应用程序中,我有一个资金模型

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"}
4

1 回答 1

2

尝试从 GlAccount 类中的存在真实验证中删除fund_id。

validates :agency_id, :name, :gl_account_number, :active, :user_stamp, :account_type, :presence => true

并且也不要将fund_id添加为隐藏字段,因为,你是对的,'fields_for'会自动处理这个问题,但这会在验证后发生。

所以你不需要 fund_id 来验证存在。

更新

此外,为了确保fund_id 永远不会为空,您可以在数据库表中放置一个约束。使用以下代码创建迁移。

change_column :gl_accounts, :fund_id, :integer, :null => false

更新 2

为确保资金存在,您需要检查是否存在资金而不是fund_id。

validates :fund, :presence => true

为此,您需要声明与“inverse_of”的关联,如下所示。

class Fund < ActiveRecord::Base
  has_many :gl_accounts, inverse_of: :fund
  accepts_nested_attributes_for :gl_accounts
end

class GlAccount < ActiveRecord::Base
  belongs_to :fund, inverse_of: :gl_accounts
  validates_presence_of :fund
end

有关更多详细信息,请参阅本指南。 http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Validating+the+presence+of+a+parent+model

于 2013-08-17T17:18:18.447 回答