Permalink
Browse files

Merged pull request #331 from daphonz/2-3-stable.

Dynamic find_or_create_by_x_and_y always creates new records in Rails 2.3.11
  • Loading branch information...
2 parents b0be721 + 9f7ff62 commit f424efe97f69910be2b93d33e2511ebf5b072a9a @josevalim josevalim committed Apr 28, 2011
@@ -381,7 +381,7 @@ def method_missing(method, *args, &block)
when /^find_or_create_by_(.*)$/
rest = $1
find_args = pull_finder_args_from(DynamicFinderMatch.match(method).attribute_names, *args)
- return send("find_by_#{rest}", find_args) ||
+ return send("find_by_#{rest}", *find_args) ||
method_missing("create_by_#{rest}", *args, &block)
when /^create_by_(.*)$/
return create($1.split('_and_').zip(args).inject({}) { |h,kv| k,v=kv ; h[k] = v ; h }, &block)
@@ -82,6 +82,15 @@ def test_find_or_create_by_with_additional_parameters
assert_equal 4, post.comments.length
end
+ def test_find_or_create_by_with_same_parameters_creates_a_single_record
+ author = Author.first
+ assert_difference "Post.count", +1 do
+ 2.times do
+ author.posts.find_or_create_by_body_and_title('one', 'two')
+ end
+ end
+ end
+
def test_find_or_create_by_with_block
post = Post.create! :title => 'test_find_or_create_by_with_additional_parameters', :body => 'this is the body'
comment = post.comments.find_or_create_by_body('other test comment body') { |comment| comment.type = 'test' }

0 comments on commit f424efe

Please sign in to comment.