Behavior of first_or_create

I just ran across a weird glitch (IMHO) in find_or_create. The arguments passed to it are *not* added to the conditions for the 'first' part. This is odd, given that it's intended to replace find_or_create_by_* methods, which *did* use the specified values as conditions.

I'm unsure on whether this behavior is entirely undesirable, but it's definitely not what I had expected in my use case (ensuring that a join table record exists). Perhaps there should be a variant (find_or_create_exactly, perhaps?) that uses the supplied attributes as additional conditions.

If nothing else, the documentation should be updated to reflect this scenario - the last case in the examples *almost* describes this scenario, but the block form used there makes it unclear what:

User.where(:first_name => 'Scarlett').first_or_create(:last_name => "O'Hara")

would do.

I also found that DataMapper implements the behavior I was expecting in their first_or_create method:


--Matt Jones

I would definitely expect

User.where(:first_name => ‘Scarlett’).first_or_create(:last_name => “O’Hara”)

to call

User.create(:first_name => ‘Scarlett’, :last_name => “O’Hara”)

if that’s not what’s happening then I think it’s a bug and should be fixed. Matt, do you have a minimal app that shows the problem? I would like to write a test case for Rails that shows this strange behavior.

Nope, that's not exactly what I observed; I'll try again. That code *does* call the create correctly, if there are no users with the correct first_name. The confusing part to me was that it wasn't quite the same as this:

User.where(:first_name => 'Scarlett').find_or_create_by_last_name("O'Hara")

The latter includes a condition on last_name in the find, where the former does not.

Given that the dynamic form is deprecated (targeted for removal in 4.1 - see active_record_deprecated_finders for details), it's worth either matching the old behavior or clearly documenting the difference to avoid confused upgraders.

--Matt Jones

This behaviour is intentional. The dynamic version did actually previously take an options hash of stuff that would get passed to create. So:

User.where(:first_name => 'Scarlett').find_or_create_by_last_name("O'Hara", :age => 32)

would do:

User.where(:first_name => "Scarlett", :last_name => "O'Hara")

and then:

User.create(:first_name => "Scarlett", :last_name => "O'Hara", :age => 32)

I believe the rationale is simply that you can put all of your conditions in a where()

Who's got two thumbs and didn't know about this feature? THIS GUY! :slight_smile:

I also noted, after actually RTFM, that this works too:

User.find_or_create_by_last_name(:last_name => "O'Hara', :age => 32)

where the named parameter in the dynamic matcher is selected out of the hash that's passed in.

Any objections to adding an explicit "converting from dynamic finders" section to the documentation for first_or_create? I'll try to get something together this weekend.


--Matt Jones