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.

[PATCH] Finding records by ID and passing blank :condtions option throws ActiveRecord::StatementInvalid

#3425

When trying to find a record using an ID or an array of IDs, and a blank conditions options, an incorrect SQL statement is generated (for example

Person.find 1, :conditions => []

attempts to execute the following SQL:

SELECT * FROM person WHERE (person.id = 1 AND ())

which throws ActiveRecord::StatementInvalid.

This is in contrast to other find behaviour, for example:

Person.find :all, :conditions => []

which works as expected, and returns all records.

I have attached a patch that corrects this, with tests. This is my first Rails patch so any suggestions/criticisms welcome!

Thanks,
Dan

Reported by Dan · October 25th, 2009 @ 02:03 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Dan
    Dan
    • Title changed from Finding records by ID and passing blank :condtions option throws ActiveRecord::StatementInvalid to [PATCH] Finding records by ID and passing blank :condtions option throws ActiveRecord::StatementInvalid

    October 25th, 2009 @ 02:13 PM

  2. Matt Jones
    Matt Jones

    This may be a larger design issue - there are a couple other spots (in the associations code) where this exact pattern comes up. I could see an alternative fix that sanitizes an empty :conditions array to a SQL no-op, "1=1" for instance. You may want to bring this up on rails-core.

    October 26th, 2009 @ 03:35 AM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to stale
    • Importance changed from to

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

    October 6th, 2010 @ 06:37 AM