覆盖rails中的delete_all

发布于 2024-10-14 01:00:48 字数 169 浏览 4 评论 0原文

我想向 delete_all... 添加 :limit 子句,我该怎么做?

我能想到的一种方法是覆盖默认的 delete_all,但我无法让它工作...... add_conditions! 函数未定义!

I want to add a :limit clause to delete_all... how can I do that?

One method I can think of is to override the default delete_all, but I cant get it to work... the add_conditions! function isn't defined!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2024-10-21 01:00:48

无需覆盖任何内容:

# Rails 2
SomeModel.scoped(:limit => 10).destroy_all

# Rails 3
SomeModel.limit(10).destroy_all

编辑:传递规范:

it "should destroy all using scope" do
  10.times { SomeModel.create }
  expect {
    SomeModel.limit(5).destroy_all
  }.to change(SomeModel, :count).by(-5)
  SomeModel.count.should == 5
end

There's no need to override anything:

# Rails 2
SomeModel.scoped(:limit => 10).destroy_all

# Rails 3
SomeModel.limit(10).destroy_all

Edit: Passing spec:

it "should destroy all using scope" do
  10.times { SomeModel.create }
  expect {
    SomeModel.limit(5).destroy_all
  }.to change(SomeModel, :count).by(-5)
  SomeModel.count.should == 5
end
帥小哥 2024-10-21 01:00:48

这适用于 mysql 和 Rails 2.3

# in config/initializers/active_record_extensions.rb
class ActiveRecord::Base
  # like delete_all, but honors limit (either through scope ar as a option)
  def self.delete_all_limit(conditions = nil, limit=nil, offset=nil)
    scope = scope(:find)
    sql = "DELETE FROM #{quoted_table_name} "
    add_conditions!(sql, conditions, scope)
    add_limit!(sql, {:limit => limit, :offset => offset}, scope)
    connection.delete(sql, "#{name} Delete all limited")
  end
end

,甚至适用于 oracle_enhanced adpater 的更丑陋的 hack。 (我认为这对于rails3来说更好)

# in config/initializers/active_record_extensions.rb

module ActiveRecord
  class Base
    # like delete_all, but honors limit and offset (either through scope ar as a option)
    def self.delete_all_limit(conditions = nil, limit=nil, offset=nil)
      scope = scope(:find)
      q = self
      limit ||= scope[:limit] if scope

      if limit
        limit = limit.to_i
         q = q.scoped(:conditions => "rownum <= #{limit}")
      end

      return q.delete_all(conditions)
    end
  end
end

This worked with mysql and rails 2.3

# in config/initializers/active_record_extensions.rb
class ActiveRecord::Base
  # like delete_all, but honors limit (either through scope ar as a option)
  def self.delete_all_limit(conditions = nil, limit=nil, offset=nil)
    scope = scope(:find)
    sql = "DELETE FROM #{quoted_table_name} "
    add_conditions!(sql, conditions, scope)
    add_limit!(sql, {:limit => limit, :offset => offset}, scope)
    connection.delete(sql, "#{name} Delete all limited")
  end
end

and an even uglier hack for oracle_enhanced adpater. (I assume this is better with rails3)

# in config/initializers/active_record_extensions.rb

module ActiveRecord
  class Base
    # like delete_all, but honors limit and offset (either through scope ar as a option)
    def self.delete_all_limit(conditions = nil, limit=nil, offset=nil)
      scope = scope(:find)
      q = self
      limit ||= scope[:limit] if scope

      if limit
        limit = limit.to_i
         q = q.scoped(:conditions => "rownum <= #{limit}")
      end

      return q.delete_all(conditions)
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文