mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
fixed joining of attributes when using find_or_create_by with multiple attributes through an association
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
This commit is contained in:
parent
aa24ea41d0
commit
9d109302f1
2 changed files with 10 additions and 1 deletions
|
@ -422,7 +422,7 @@ module ActiveRecord
|
|||
match = DynamicFinderMatch.match(method)
|
||||
if match && match.creator?
|
||||
attributes = match.attribute_names
|
||||
return send(:"find_by_#{attributes.join('and')}", *args) || create(Hash[attributes.zip(args)])
|
||||
return send(:"find_by_#{attributes.join('_and_')}", *args) || create(Hash[attributes.zip(args)])
|
||||
end
|
||||
|
||||
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
||||
|
|
|
@ -167,6 +167,15 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? }
|
||||
end
|
||||
|
||||
def test_dynamic_find_or_create_from_two_attributes_using_an_association
|
||||
author = authors(:david)
|
||||
number_of_posts = Post.count
|
||||
another = author.posts.find_or_create_by_title_and_body("Another Post", "This is the Body")
|
||||
assert_equal number_of_posts + 1, Post.count
|
||||
assert_equal another, author.posts.find_or_create_by_title_and_body("Another Post", "This is the Body")
|
||||
assert !another.new_record?
|
||||
end
|
||||
|
||||
def test_cant_save_has_many_readonly_association
|
||||
authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
|
||||
authors(:david).readonly_comments.each { |c| assert c.readonly? }
|
||||
|
|
Loading…
Reference in a new issue