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.


On Tue, Jul 24, 2012 at 2:43 PM, Matt Jones <al2o...@gmail.com> wrote:

> 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:
>
> https://github.com/datamapper/dm-core/blob/master/lib/dm-core/model.rb#L448
>
> Thoughts?
>
> --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.
>
>

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