在rails 2.3.2中调用associated_model.create()之后没有创建对象

时间:2012-07-02 06:52:44

标签: ruby-on-rails ruby activerecord associations

我一直被困在一个看似简单的事情上,但是已经两天了,我无法弄清楚我错过了什么,我相信它是一件小事。

我的系统有registrationsregistration has_many registration_transactions

首先将注册创建为incomplete,然后注册checkout,并且注册处于pending状态。在checkout方法中,应该创建registration_transaction

注册和registration_tranasction都有一个字段payment_method,如果payment_method为Credit Cardtransaction_typepayment,则会为同一注册创建另一个registration_transaction,其中transaction_type为Fee

这是执行所有这些操作的代码:

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有两个交易,一个用于费用,另一个用于支付,就像期望的那样。但是当注册完成并且我查看数据库时,只有付款交易。不知何故费用交易没有得到保存。

1 个答案:

答案 0 :(得分:0)

在保存之前,此行不会保存构建关联模型。

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

然后,

payment.update_attributes(...)

更新DB中的现有记录。您需要使用所有属性构建关联记录,然后保存。