build_foo 调用中的第二个参数永远不会进入Foo#initialize
(即args[1]
is nil
)。有什么建议可以让两个或多个参数传入Foo#initialize
,同时保持*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