Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8976: exists_should_use_count.patch

File exists_should_use_count.patch, 1.3 kB (added by veader, 1 year ago)

use count instead of find for further performance gains

  • test/base_test.rb

    old new  
    17041704    assert_equal %("#{t.written_on.to_s(:db)}"), t.attribute_for_inspect(:written_on) 
    17051705    assert_equal '"This is some really long content, longer than 50 ch..."', t.attribute_for_inspect(:content) 
    17061706  end 
     1707   
     1708  def test_exists_should_work_with_id 
     1709    t = topics(:first) 
     1710    assert Topic.exists?(t.id) 
     1711  end 
     1712   
     1713  def test_exists_should_work_with_conditions 
     1714    t = topics(:first) 
     1715    assert Topic.exists?(:title => t.title) 
     1716  end 
     1717   
     1718  def test_exists_should_return_false 
     1719    assert !Topic.exists?(800) 
     1720  end 
    17071721 
    17081722  private 
    17091723    def assert_readers(model, exceptions) 
  • lib/active_record/base.rb

    old new  
    447447      #   Person.exists?(:name => "David") 
    448448      #   Person.exists?(['name LIKE ?', "%#{query}%"]) 
    449449      def exists?(id_or_conditions) 
    450         !find(:first, :conditions => expand_id_conditions(id_or_conditions)).nil? 
     450        count(:conditions => expand_id_conditions(id_or_conditions)) > 0 
    451451      rescue ActiveRecord::ActiveRecordError 
    452452        false 
    453453      end