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.

find_or_create_by on an association always creates new records

#6535

Calling a dynamic find_or_create_by_ method on an association always ends up creating a new record because it improperly queries the database when attempting to find the record with the given values.

Attached is a test on a has_many association demonstrating this problem, as well as a simple patch that fixes it (spoiler: the finder arguments were passed incorrectly from a recent patch on this method).

Reported by Casey Dreier · March 5th, 2011 @ 09:23 PM

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

Activity

  1. Casey Dreier
    Casey Dreier

    Just to be clear, this bug report and patch are for Rails 2.3.11.

    March 5th, 2011 @ 09:28 PM

  2. Jon Jensen
    Jon Jensen

    In addition to the incorrect behavior, it can also cause unhandled exceptions for postgres. For example, company.employees.find_or_create_by_group_id_and_name(1, 'bob') would generate some SQL like "... group_id IN (1, 'bob') AND name IS NULL". In postgres you get an error, since 'bob' hasn't been explicitly cast to an integer.

    March 22nd, 2011 @ 07:44 PM

  3. Leon Miller-Out