Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic find_or_create_by_x_and_y always creates new records in Rails 2.3.11 #331

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def method_missing(method, *args, &block)
when /^find_or_create_by_(.*)$/ when /^find_or_create_by_(.*)$/
rest = $1 rest = $1
find_args = pull_finder_args_from(DynamicFinderMatch.match(method).attribute_names, *args) 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) method_missing("create_by_#{rest}", *args, &block)
when /^create_by_(.*)$/ when /^create_by_(.*)$/
return create($1.split('_and_').zip(args).inject({}) { |h,kv| k,v=kv ; h[k] = v ; h }, &block) return create($1.split('_and_').zip(args).inject({}) { |h,kv| k,v=kv ; h[k] = v ; h }, &block)
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ def test_find_or_create_by_with_additional_parameters
assert_equal 4, post.comments.length assert_equal 4, post.comments.length
end 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 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' 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' } comment = post.comments.find_or_create_by_body('other test comment body') { |comment| comment.type = 'test' }
Expand Down