build_foo調用中的第二個參數絕不會將其設置爲Foo#initialize
(即args[1]
爲nil
)。任何建議,以獲得兩個或更多的參數傳遞到Foo#initialize
,同時保持*args
初始化的唯一參數?如何在Rails 3的build_association調用中傳遞多個構造函數參數?
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
爲了您的方便:http://guides.rubyonrails.org/association_basics.html#detailed-association-reference – 2011-06-14 06:42:55