删除全部 ()

ActiveRecord::Relation

删除记录而不首先实例化记录,因此不调用 #destroy 方法或调用回调。 这是一条直接进入数据库的 SQL DELETE 语句,比 destroy_all. 但要小心关系,特别是

:dependent
不遵守在关联上定义的规则。 返回受影响的行数。

<code class="language-css">

<figure class="highlight"><pre><code class="language-html" data-lang="html">Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all</code></pre></figure>

两个调用都使用一条 DELETE 语句一次性删除所有受影响的帖子。 如果您需要销毁依赖关联或致电您的

before_*
或者
after_destroy
回调,使用 destroy_all代替方法。

如果提供了无效的方法, delete_all引发 ActiveRecordError:

<code class="language-ruby">

<figure class="highlight"><pre><code class="language-html" data-lang="html">Post.distinct.delete_all
# =<span class="ni">&amp;gt;</span> ActiveRecord::ActiveRecordError: delete_all doesn't support distinct</code></pre></figure>

delete_all() Link

Deletes the records without instantiating the records first, and hence not calling the #destroy method nor invoking callbacks. This is a single SQL DELETE statement that goes straight to the database, much more efficient than destroy_all. Be careful with relations though, in particular

:dependent
rules defined on associations are not honored. Returns the number of rows affected.

Post.where(person_id: 5).where(category: ['Something', 'Else']).delete_all

Both calls delete the affected posts all at once with a single DELETE statement. If you need to destroy dependent associations or call your

before_*
or
after_destroy
callbacks, use the destroy_all method instead.

If an invalid method is supplied, delete_all raises an ActiveRecordError:

Post.distinct.delete_all
# =&gt; ActiveRecord::ActiveRecordError: delete_all doesn't support distinct

Source: show | on GitHub

 

 How To Use IRB to Explore Ruby | DigitalOceanhttps://www.digitalocean.com/community/tutorials/how-to-use-irb-to-explore-rubyirb 的使用

是通用的ruby环境