Rails - 模型不通过方法保存属性,但如果手动分配(在控制台中)

Posted

技术标签:

【中文标题】Rails - 模型不通过方法保存属性,但如果手动分配(在控制台中)【英文标题】:Rails - Model doesn't save attributes through method, but does if assigned by hand (in Console) 【发布时间】:2012-05-14 18:28:13 【问题描述】:

我有一个模型Payment,用于跟踪信用卡交易。现在我试图让它映射一些 PayPal 在成功的信用卡交易中返回的值。这是由notify = ActiveMerchant::Billing::Integrations::Paypal::Notification.new(raw_post) 处理的(太长了)

但是,我编写的方法在控制台中不起作用。但是,如果我直接分配它们,它就可以工作。

例如Rails 控制台

notify = ActiveMerchant::Billing::Integrations::Paypal::Notification.new(raw_post)
# Works
payment = Payment.new    
payment.raw_response = notify.raw
save! # Save is successful

# Does not work
payment = Payment.new
payment.map_paypal_return(notify) # save! call in this method does not trigger errors.

请看看我的课。

class Payment < ActiveRecord::Base
  belongs_to :order


  def map_paypal_return(notify)
    puts "in mapping" => Outputted
    raw_response = notify.raw
    payment_status = notify.status

    order_identifier = notify.item_id # Map ID to ID as a safety check bah.

    payer_email = notify.params["payer_email"]
    receiver_account = notify.account
    auth_mode = notify.params["payment_type"]

    transaction_identifier = notify.invoice
    currency = notify.currency
    amount = notify.gross
    puts self.attributes # Returns attributes without the above assignments

    save! # No errors triggered =(
  end
end

控制台中的方法调用

1.9.2p318 :032 > notify.invoice # This should have been mapped to transaction_identifier
 => "2012-146" 

1.9.2p318 :031 > p.map_paypal_return(notify)
in mapping
"id"=>nil, "order_id"=>nil, "order_identifier"=>nil, "card_holder_name"=>nil, "auth_mode"=>nil, "amount"=>nil, "amount_string"=>nil, "currency"=>nil, "merchant_reference"=>nil, "transaction_identifier"=>nil, "status"=>nil, "other_errors"=>nil, "received_date"=>nil, "deleted"=>nil, "created_at"=>nil, "updated_at"=>nil, "state"=>"checkout", "payment_type"=>nil, "raw_response"=>nil, "receiver_account"=>nil, "payer_email"=>nil
  SQL (47.4ms)  INSERT INTO "payments" ("amount", "amount_string", "auth_mode", "card_holder_name", "created_at", "currency", "deleted", "merchant_reference", "order_id", "order_identifier", "other_errors", "payer_email", "payment_type", "raw_response", "received_date", "receiver_account", "state", "status", "transaction_identifier", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["amount", nil], ["amount_string", nil], ["auth_mode", nil], ["card_holder_name", nil], ["created_at", Sat, 05 May 2012 20:32:33 SGT +08:00], ["currency", nil], ["deleted", nil], ["merchant_reference", nil], ["order_id", nil], ["order_identifier", nil], ["other_errors", nil], ["payer_email", nil], ["payment_type", nil], ["raw_response", nil], ["received_date", nil], ["receiver_account", nil], ["state", "checkout"], ["status", nil], ["transaction_identifier", nil], ["updated_at", Sat, 05 May 2012 20:32:33 SGT +08:00]]
 => true 

我不知道我是否在某个地方对 Ruby/Rails 产生了愚蠢的基本误解。非常感谢您的帮助。

【问题讨论】:

【参考方案1】:

我注意到你的map_paypal_return 方法中有

raw_response = notify.raw

raw_response 是对象属性之一时。要在相应类的方法中分配 ActiveRecord 提供的属性,您需要这样做

self.raw_response = notify.raw

对于方法中属性的其他分配也是如此。

这是 ruby​​ 的限制。属性方法通过定义attribute= 方法(例如raw_response=)来实现,并且没有限定符(obj.self.),ruby 假定语句只是一个局部变量赋值。

【讨论】:

我一直认为 self 关键字在它是实例方法时不是必需的。哦该死,最后几周的代码! 你拯救了我的一天,谢谢!稍后将删除多余的 cmets。愚蠢的关系one.argh。

以上是关于Rails - 模型不通过方法保存属性,但如果手动分配(在控制台中)的主要内容,如果未能解决你的问题,请参考以下文章

保存 Rails 模型的所有属性

从视图编辑模型上的多个属性的 Rails 方法

如何在rails中保存包含另一个模型属性的模型?

Rails、Stripe 和 Subscriptions - 模型

如何保存模型,而在Rails的运行回调

Rails:更新模型属性而不调用回调