2016-12-14 4 views
0

内部の更新、私は私のモデル内の更新を作りたいRailsのモデルと、ArgumentError

self => BookingService(id: integer, currency_code: string, total_price: integer, group_total: integer, other attrs) 

が、私はupdate(total_price:1,group_total:1)を作ったとき、それが起きているのはなぜ

ArgumentError: wrong number of arguments (1 for 2) 


[37] pry(BookingService)> update(total_price:1,group_total:1) 
ArgumentError: wrong number of arguments (1 for 2) 
from /Users/xx/.rvm/gems/[email protected]_api/gems/activerecord-4.1.13/lib/active_record/relation.rb:352:in `update' 

[38] pry(BookingService)> self.update(total_price:1,group_total:1) 
ArgumentError: wrong number of arguments (1 for 2) 
from /Users/xxx/.rvm/gems/[email protected]_api/gems/activerecord-4.1.13/lib/active_record/relation.rb:352:in `update' 

を持っていますか?ここで

+0

が 'ここで何をself'されますか?モデルメソッドを呼び出して属性を更新する方法を示すコードを追加します。 – dp7

答えて

0

updateメソッドのソースコードです:あなたが見ることができるように

def update(id, attributes) 
    if id.is_a?(Array) 
    idx = -1 
    id.collect { |one_id| idx += 1; update(one_id, attributes[idx]) } 
    else 
    object = find(id) 
    object.update_attributes(attributes) 
    object 
    end 
end 

、この方法は、レコードへの参照を期待しています。

代わりにこれをやってみてください。

self.update(...) 

selfへの参照はあなたの問題を解決する必要があります。彼の方法はClassMethodあるおそらくので、

http://apidock.com/rails/ActiveRecord/Base/update/class

編集

OPは解決策を試してみましたが、それはまだ動作しませんでした。

代わりにこれを試してみてください:

update(id, {attr}) 
+0

'self'は役に立たなかった – user

+0

@user updated答え。 – fbelanger

+0

これは 'id'、tnxで動作します。しかし、私は問題の原因を調査する必要があります。 – user