2012-07-02 19 views
0

我被困在一個看似簡單的事情上,但已經過了兩天,我找不到我失蹤的東西,我相信它是一件小事。在rails中的associated_model.create()調用後沒有創建對象2.3.2

我的系統有registrationsregistrationhas_many registration_transactions

首先創建一個註冊爲incomplete然後是checkout並且註冊處於pending狀態。在checkout方法中,應該創建registration_transaction

登記和registration_tranasction有一個字段payment_method,如果PAYMENT_METHOD是Credit Cardtransaction_typepayment則在同一註冊,其中TRANSACTION_TYPE是Fee

這裏創造了另一個registration_transaction是,做這一切的代碼:

if !self.valid? 
    errors.add_to_base("Error encountered processing transaction: Invalid Registration Data Entered.") 
    else 
    #if self.registration_transactions.empty? 
     registration_transactions.create(
     :transaction_type => 'Fee', 
     :payment_method_id => self.payment_method_id, 
     :amount    => event_registration_type_membership.price, 
     :notes    => "#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee", 
     :ip_address   => self.ip_address, 
     :status    => 'Processed', 
     :date_1    => Date.current) 
    #end 


    if payment_method.name.eql? 'Credit Card' 
     payment = registration_transactions.find_by_notes("#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee Initial Payment") 
     #Commented the line below and changed status to 'Processed'. 
     #Client wanted to process a transaction automatically when a user registers for an event and pays through CC 
     #payment = registration_transactions.build(:status => 'Draft') if payment.nil? 
     payment = registration_transactions.build(:status => 'Processed') if payment.nil? 



     payment.update_attributes(
     :transaction_type    => 'Payment', 
     :payment_method_id   => self.payment_method_id, 
     :amount      => event_registration_type_membership.price, 
     :notes      => "#{event.code} - #{event_registration_type_membership.event_registration_type.name} Registration Fee Initial Payment", 
     :credit_card_number   => self.credit_card_number, 
     :credit_card_security_code => self.credit_card_security_code, 
     :ip_address     => self.ip_address, 
     #:status      => 'Draft', changed status to processed 
     :status      => 'Processed', 

     :credit_card_type_id   => self.credit_card_type_id, 
     :credit_card_expiration_date => self.credit_card_expiration_date, 
     :billing_city     => self.transaction.billing_city, 
     :billing_state    => self.transaction.billing_state, 
     :billing_postal_code   => self.transaction.billing_postal_code, 
     :billing_country    => self.transaction.billing_country, 
     :billing_first_name   => self.billing_first_name, 
     :billing_last_name   => self.billing_last_name, 
     :billing_address_1   => self.transaction.billing_address_1, 
     :date_1      => Date.current) 

     result = payment.process(self.ip_address,false) 
     logger.info "GCS - attempted to process payment via authorize.net, RESULT=" + result.to_s 
     logger.info "GCS - messages from authorize net=" + payment.errors.full_messages.to_s 


     logger.info "GCS - result: #{result.inspect}" 
     logger.info "GCS - payment: #{payment.inspect}" 
     logger.info "GCS - payment.errors: #{payment.errors.full_messages}" 
     self.save! 

     errors.add_to_base(payment.errors.full_messages) if !result 



    else #check payment 
     result = true 
    end 
    end 

問題是,當我把檢查點和檢查運行時,然後registration_transactions有兩個交易,一個爲費用和另一個支付,像所期望的那樣。但是當註冊完成時,我查看數據庫,只有付款交易。費用交易不知道如何保存。

回答

0

該行在保存之前不會保存構建關聯模型。

registration_transactions.build(:status => 'Processed') 

然後,

payment.update_attributes(...) 

這在數據庫更新現有記錄。您需要使用所有屬性構建關聯記錄,然後保存。

相關問題