加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 当用作param时的’*`的含义(不像* arg,只是*)[

发布时间:2020-12-16 19:23:27 所属栏目:百科 来源:网络整理
导读:参见英文答案 naked asterisk as parameter in method definition: def f(*)1个 当我阅读Rails代码时,我发现了这一点 def save(*) create_or_update || raise(RecordNotSaved)end *做什么? :o 我知道当我们像* args一样使用它时会发生什么,但在这种情况下,
参见英文答案 > naked asterisk as parameter in method definition: def f(*)1个
当我阅读Rails代码时,我发现了这一点
def save(*)
  create_or_update || raise(RecordNotSaved)
end

*做什么? :o
我知道当我们像* args一样使用它时会发生什么,但在这种情况下,它只是简单*.

ref https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L119

解决方法

在这种特定情况下,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读