当我阅读Rails代码时,我发现了这个
def save(*)
create_or_update || raise(RecordNotSaved)
end
*
的作用是什么?:O我知道当我们像*args
一样使用它时会发生什么,但在这种情况下,它只是普通的*
。
参考https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L119
与参数名一起使用时的含义相同:吞噬所有剩余的参数。但是,由于没有可将它们绑定到的名称,因此无法访问这些参数。换句话说:它接受任意数量的参数,但忽略所有参数。
请注意,实际上有一种使用参数的方法:当您在没有参数列表的情况下调用super
时,参数会按原样转发到超类方法。
在这种特定情况下,save不接受任何参数。这就是赤裸裸的泼溅。但是,正如您可能知道的,在ActiveRecord
模型上调用save接受选项,因为此方法在此处被ActiveRecord::Validations
覆盖:
https://github.com/rails/rails/blob/v3.1.3/activerecord/lib/active_record/validations.rb#L47
# The validation process on save can be skipped by passing <tt>:validate => false</tt>. The regular Base#save method is
# replaced with this when the validations module is mixed in, which it is by default.
def save(options={})
perform_validations(options) ? super : false
end