On Jul 24, 2012, at 5:42 PM, Jon Leighton wrote:

> On 24/07/12 21:29, Matt Jones wrote:
>> 
>> 
>> 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.
> 
> 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! :)

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.

Thanks,

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to rubyonrails-core@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-core+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en.

Reply via email to