Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord::Base exists? expects argument

#1817

Tiny but annoying bug if you want to have beautiful code:

The Bug By now ActiveRecord::Base method "exists?" expects an argument.

Example: to check if at least one entry exists in table 'persons':


Person.exists?  

But this doesn't work, as an argument is expected. Even more common scenario happens with named scope:


Person.rails_developer.exists?  

The Workaround As a workaround passing an empty hash works, but looks ugly:


Person.exists?({})  

The Fix So my proposal to get back to beautiful code: Add empty hash as default parameter to 'extists?' method:


def exists?(id_or_conditions = {} )
  ...
end

Cheers Tobi

Reported by Tobi · January 29th, 2009 @ 10:50 AM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Scott Taylor
  2. Scott Taylor
  3. Tobi
    Tobi
    • Tag changed from argument, default, exists, missing to argument, default, exists, missing, patch

    cheers!

    February 1st, 2009 @ 02:14 PM

  4. Repository
    Repository
    • State changed from new to committed

    (from [5a8f764661bcdf9c6ce503c0ff343a1970deb1bb]) Add ActiveRecord::Base.exists? with no args [#1817 ActiveRecord::Base exists? expects argument state:committed]

    Signed-off-by: David Heinemeier Hansson david@loudthinking.com http://github.com/rails/rails/co...

    February 5th, 2009 @ 07:51 PM