Thursday, December 27, 2007

ActiveRecord::Base#exists?

class ActiveRecord::Base
def self.exists?(conditions, unique_id = nil)
count1(conditions, unique_id)>0
end
end

Returns true/false if at least one row that follows ceratin conditions is found

  • Can only be used along with count1
  • unique_id - used when you still want to allow a certain raw (defined by id...) to be left out of the count. Useful for validations, ie:

    User.exists? ['nick != ?',user.nick], user.id


0 comments: