2015-01-09 44 views
0

我有一個名爲Transaction的嵌套表單,其中包含一個名爲Banking Information Form的子對象。當父母(交易)transaction_mechanism字段設置爲「檢查」時,不應觸發銀行信息驗證。出於某種原因,當我將表單中的transaction_mechanism設置爲「check」時,它仍然調用銀行信息表單驗證。在bank_information_form模型中,我有一個名爲cheque_transaction的方法?如果transaction_mechanism是「check」,應該防止觸發驗證,但是我收到一個錯誤。在條件驗證中沒有給出塊(yield)rails錯誤

錯誤

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 

結束

+0

1 「的時候,父母的(交易)transaction_mechanism字段設置爲銀行業信息驗證應該只觸發」檢查「 2.由於某種原因,當我在表單中設置transaction_mechanism爲」check「時,它仍然調用銀行信息表單驗證「 因此,transaction_mechanism設置爲'檢查'類型,所以它被調用?那麼問題是什麼? – Ajay 2015-01-09 23:38:58

+0

@Ajay謝謝你指出。修復這個問題是有道理的! – Questifer 2015-01-09 23:44:31

回答

0

一些經過試用和錯誤 - 我發現了一個方法來讓我的條件驗證工作。我首先將模型名稱從'Transaction'更改爲'AccountTransaction'。我發現'Transaction'是rails中的保留字後,我這樣做是爲了遵守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 
2
class Transaction < ActiveRecord::Base 
    has_one :banking_information_form 
    # Other code 
end 

如你所述,你的銀行信息將被驗證,只有當父(transcactions)機制不「檢查」:

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 
+0

我以前在銀行信息表單模型中擁有相同的belongs_to語句,但收到大量奇怪的錯誤。在你的回答之後再進行一些研究之後,我認爲它與'Transaction'是一個保留字在軌道中有關係嗎?我可能不得不修改模型的名稱。我會回覆我的結果。謝謝! – Questifer 2015-01-10 16:51:39

+0

我更改了交易的型號名稱並複製了您的答案,並且仍然得到相同的結果。 – Questifer 2015-01-11 15:59:40