build_foo 呼び出しの 2 番目の引数は決してFoo#initialize
(つまりargs[1]
is nil
) になりません。初期化する唯一の引数をFoo#initialize
保持しながら、2 つ以上の引数を渡すための提案はありますか?*args
class Foo < ActiveRecord::Base
belongs_to :bar
def initialize *args
super()
self.total = args[0] + args[1]
end
end
class Bar < ActiveRecord::Base
has_one :foo
def do_something
build_foo 2, 3 # 3 never reaches Foo#initialize
build_foo [2,3] # args[0] == [2,3], which is not what's desired
save!
end
end