Skip to content
This repository

find_or_create_by on an association always creates new records #906

Closed
lighthouse-import opened this Issue May 15, 2011 · 4 comments

1 participant

Lighthouse Import
Lighthouse Import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6535
Created by Casey Dreier - 2011-03-05 21:23:59 UTC

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).

Lighthouse Import

Imported from Lighthouse.
Comment by Casey Dreier - 2011-03-05 21:28:05 UTC

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

Lighthouse Import

Imported from Lighthouse.
Comment by Jon Jensen - 2011-03-22 19:44:14 UTC

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.

Lighthouse Import

Imported from Lighthouse.
Comment by Leon Miller-Out - 2011-03-25 19:39:07 UTC

+1

Lighthouse Import

Attachments saved to Gist: http://gist.github.com/971806

Lighthouse Import lighthouse-import closed this May 21, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.