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.

BUG: Query Cache does not handle multiple calls to find_or_create_by_*

#2492

Greetings.

I came across this bug today in Rails 2.2.2.

Making subsequent calls to a find_or_create_by_* within the lifetime of one request does not behave as expected. The first call is correct, but the second call, because the generated SQL is the exact same, uses the query cache, even if the first call has already created a record. The result is that the second and subsequent calls will create additional records, which is unexpected.

Simple example:

User.find_or_create_by_user_id_and_name(123, "Rails") # Record created here User.find_or_create_by_user_id_and_name(123, "Rails") # Query cache hit; another record created

-Chris

Reported by findchris · April 14th, 2009 @ 09:33 PM

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

Activity

  1. findchris
    findchris

    Formatting fix:

    
    User.find_or_create_by_user_id_and_name(123, "Rails") # Record created here
    User.find_or_create_by_user_id_and_name(123, "Rails") # Query cache hit; another record created
    

    April 14th, 2009 @ 09:34 PM

  2. Max Lapshin
    Max Lapshin

    Is it reproduced in 2.3.2?

    April 23rd, 2009 @ 11:03 AM

  3. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.2.2, active_record, cache, find, finder, find_or_create, query, query_cache to 2.2.2, active_record, bugmash, cache, find, finder, find_or_create, query, query_cache

    August 6th, 2009 @ 02:10 PM

  4. Rizwan Reza
    Rizwan Reza

    not reproducible

    I created a test app and checked it. find_or_create_by works as expected.

    August 7th, 2009 @ 08:42 PM

  5. Pratik
    Pratik
    • State changed from new to invalid

    Works fine for me too.

    @findchris Could you please upload a failing test case if this is still an issue ?

    Thanks.

    August 8th, 2009 @ 03:01 AM

  6. CancelProfileIsBroken
    CancelProfileIsBroken
    • Tag changed from 2.2.2, active_record, bugmash, cache, find, finder, find_or_create, query, query_cache to 2.2.2, active_record, cache, find, finder, find_or_create, query, query_cache
    • Milestone cleared.

    August 8th, 2009 @ 11:06 PM