条件验证时没有给出(yield)rails阻塞的块

时间:2015-01-09 23:21:59

标签: ruby-on-rails ruby-on-rails-4

我有一个名为Transaction的嵌套表单,其中包含一个名为Banking Information Form的子对象。当父(事务)transaction_mechanism字段设置为“check”时,不应触发银行信息验证。出于某种原因,当我在表单中将transaction_mechanism设置为“check”时,它仍在调用银行信息表单验证。在bank_information_form模型中,我有一个名为cheque_transaction的方法?如果transaction_mechanism是“检查”,这应该阻止验证触发,但我收到错误。

错误

no block given (yield)

transaction.rb

class Transaction < ActiveRecord::Base
  # Require packages
  require 'time'

  # Associations
  belongs_to :user
  has_one :banking_information_form

  # Nested attributes
  accepts_nested_attributes_for :banking_information_form

  # Validations
  validates_associated :banking_information_form
  validate  :check_debit_or_credit
  validates :transaction_mechanism, :transaction_kind, :debit, :salesforce_id, presence: true
  validate  :debit, :credit,numericality: { only_integer: true }
end

banking_information_form.rb

class BankingInformationForm < ActiveRecord::Base
  # Associations
  # Belongs to :transaction
  belongs_to :owner, foreign_key: "transaction_id", class_name: "Transaction"

  # Validations
  validates :financial_institution, :transit_number, :bank_account_number, presence: true, :unless =>    :cheque_transaction?
  validates :transit_number, length: {is: 5,
                                  too_short: "Your transit number is 5 digits long.",
                                  too_long: "Your transit number is 5 digits long."}
  validates :bank_account_number, length: {minimum: 4, maximum: 12,
                                  too_short: "Your bank account number will be between 4 and 12 digits long.",
                                  too_long: "Your bank account number will be between 4 and 12 digits long."}

  private

    def cheque_transaction?
      self.transaction.transaction_mechanism == "Cheque"
    end

2 个答案:

答案 0 :(得分:2)

class Transaction < ActiveRecord::Base
  has_one :banking_information_form
  # Other code 
end

正如您所描述的那样,只有当父母(交易)机制不是“检查”时,您的银行信息才会得到验证。 :

class BankingInformationForm < ActiveRecord::Base
  belongs_to :transaction #this was missing in your code.

  validates :financial_institution, :transit_number, 
    :bank_account_number, presence: true, unless: :cheque_transaction?
  # other validations here 

  private 
   def cheque_transaction?
     transaction.transaction_mechanism == "Cheque"
   end
end

答案 1 :(得分:0)

经过一些更多的试验和错误 - 我找到了一种让我的条件验证工作的方法。我首先更改了&#39; Transaction&#39;到帐户交易&#39;。在我发现&#39; Transaction&#39;之后,我这样做是为了遵守铁轨的惯例。是rails中的保留字。

一旦我更改了模型名称,我就使用inverse_of :: account_transaction添加了我的关联。关于inverse_of here可以阅读更多内容。

<强> account_transaction.rb

class AccountTransaction < ActiveRecord::Base
  # Require packages
  require 'time'

  # Associations
  belongs_to :user
  has_one :banking_information_form, inverse_of: :account_transaction

  # Nested attributes
  accepts_nested_attributes_for :banking_information_form

  # Validations
  validates_associated :banking_information_form
  validate  :check_debit_or_credit
  validates :transaction_mechanism, :transaction_kind, :debit, :salesforce_id, presence: true
  validate  :debit, :credit,numericality: { only_integer: true }
end

<强> banking_information_form.rb

class BankingInformationForm < ActiveRecord::Base
  # Associations
  belongs_to :account_transaction

  # Validations
  validates :financial_institution, :transit_number, :bank_account_number, presence: true, :unless => :cheque_transaction?
  validates :transit_number, length: {is: 5,
                                  too_short: "Your transit number is 5 digits long.",
                                  too_long: "Your transit number is 5 digits long."}, :unless => :cheque_transaction?
  validates :bank_account_number, length: {minimum: 4, maximum: 12,
                                  too_short: "Your bank account number will be between 4 and 12 digits long.",
                                  too_long: "Your bank account number will be between 4 and 12 digits long."}, :unless => :cheque_transaction?

  private

    def cheque_transaction?
      self.account_transaction.transaction_mechanism == "Cheque"
    end
end