2011-06-01 42 views
0

我传递这些参数到控制器:为什么执行属性赋值的顺序失败?

{ 
    "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

我可以改变它吗? 这是为什么失败?

回答

0

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

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

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"} 

,你明白我的意思,数的花括号是搞砸了,道具。身份证没有在交易中结束(我只是复制并粘贴上面粘贴的代码)

+0

是的,我明白这一点。 关键是possitive_balance确实可以通过traces_attributes =来访问,因为这个attr在事务内部,并且在哈希中你可以看到possitive_balance在事务内部。 这就是为什么即时通讯使用自己而不是参数 – 2011-06-01 22:01:29

相关问题