为什么属性赋值的执行顺序失败?

时间:2011-06-01 21:49:48

标签: ruby-on-rails ruby parameters

我将这些参数传递给控制器​​:

{
  "utf8" => "✓",
  "authenticity_token" => "ersjaJ4/ieZelVifP/YpBHTJtiQ53HgO5KYjEdW0BlQ=",
  "transaction" => {
    "use_balance" => "1",
    "traces_attributes" => {
      "trace_ids" => ["6"],
      "6" => {
        "amount" => "12.0",
        "charge_id" => "6"
      }
    },
    "positive_balance" => "12",
    "property_id" => "2",
    "community_id" => "1"
  },
  "commit" => "Save Payment",
  "community_id" => "1",
  "property_id" => "2"
}

controller#create然后:

@payment = Transaction.new(params[:transaction])

然后是交易模型:

belongs_to :property
belongs_to :community
attr_accessible :positive_balance

def traces_attributes=(params)
  #INSIDE HERE THE VALUES OF
  #params[:trace_ids] => ['6'] OK
  #BUT
  #self.possitive_balance => "" **NOT OK**
  #self.property_id => nil **NOT OK**
end

我的假设是traces_attribute=positive_balance=property_id之前执行

我可以更改吗? 为什么这会失败?

2 个答案:

答案 0 :(得分:0)

它基于事务哈希看起来property_id在该哈希之外,因此如果您基于事务构建它将不具有property_id

"transaction"=>{"use_balance"=>"1",
                "traces_attributes"=>{"trace_ids"=>["6"],
                                      "6"=>{"amount"=>"12.0",
                                            "charge_id"=>"6"
                                            }
                                      },
                "positive_balance"=>"12",
                "property_id"=>"2",
                "community_id"=>"1"
 },
"commit"=>"Save Payment",
"community_id"=>"1",
"property_id"=>"2"}

你明白我的意思吗,花括号的数量搞砸了,道具。 id不会以交易结束(我只是复制并粘贴上面粘贴的代码)

答案 1 :(得分:0)

作业的顺序应该与表格中的参数顺序相同,但我认为这不是保证。

更安全的解决方案是仅将数据存储在traces_attributes=方法中,稍后再访问其他属性,例如before_save callback