mirror of https://github.com/rails/rails
AssociationCollection#create_by_*, find_or_create_by_* work properly now. [#1108 state:resolved]
Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
parent
d7c2e52c6c
commit
3f563f1696
|
@ -409,6 +409,17 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def method_missing(method, *args)
|
||||
case method.to_s
|
||||
when 'find_or_create'
|
||||
return find(:first, :conditions => args.first) || create(args.first)
|
||||
when /^find_or_create_by_(.*)$/
|
||||
rest = $1
|
||||
return send("find_by_#{rest}", *args) ||
|
||||
method_missing("create_by_#{rest}", *args)
|
||||
when /^create_by_(.*)$/
|
||||
return create Hash[$1.split('_and_').zip(args)]
|
||||
end
|
||||
|
||||
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
||||
if block_given?
|
||||
super { |*block_args| yield(*block_args) }
|
||||
|
|
|
@ -21,6 +21,68 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
|
|||
Client.destroyed_client_ids.clear
|
||||
end
|
||||
|
||||
def test_create_by
|
||||
person = Person.create! :first_name => 'tenderlove'
|
||||
post = Post.find :first
|
||||
|
||||
assert_equal [], person.readers
|
||||
assert_nil person.readers.find_by_post_id post.id
|
||||
|
||||
reader = person.readers.create_by_post_id post.id
|
||||
|
||||
assert_equal 1, person.readers.count
|
||||
assert_equal 1, person.readers.length
|
||||
assert_equal post, person.readers.first.post
|
||||
assert_equal person, person.readers.first.person
|
||||
end
|
||||
|
||||
def test_create_by_multi
|
||||
person = Person.create! :first_name => 'tenderlove'
|
||||
post = Post.find :first
|
||||
|
||||
assert_equal [], person.readers
|
||||
|
||||
reader = person.readers.create_by_post_id_and_skimmer post.id, false
|
||||
|
||||
assert_equal 1, person.readers.count
|
||||
assert_equal 1, person.readers.length
|
||||
assert_equal post, person.readers.first.post
|
||||
assert_equal person, person.readers.first.person
|
||||
end
|
||||
|
||||
def test_find_or_create_by
|
||||
person = Person.create! :first_name => 'tenderlove'
|
||||
post = Post.find :first
|
||||
|
||||
assert_equal [], person.readers
|
||||
assert_nil person.readers.find_by_post_id post.id
|
||||
|
||||
reader = person.readers.find_or_create_by_post_id post.id
|
||||
|
||||
assert_equal 1, person.readers.count
|
||||
assert_equal 1, person.readers.length
|
||||
assert_equal post, person.readers.first.post
|
||||
assert_equal person, person.readers.first.person
|
||||
end
|
||||
|
||||
def test_find_or_create
|
||||
person = Person.create! :first_name => 'tenderlove'
|
||||
post = Post.find :first
|
||||
|
||||
assert_equal [], person.readers
|
||||
assert_nil person.readers.find(:first, :conditions => {
|
||||
:post_id => post.id
|
||||
})
|
||||
|
||||
reader = person.readers.find_or_create :post_id => post.id
|
||||
|
||||
assert_equal 1, person.readers.count
|
||||
assert_equal 1, person.readers.length
|
||||
assert_equal post, person.readers.first.post
|
||||
assert_equal person, person.readers.first.person
|
||||
end
|
||||
|
||||
|
||||
def force_signal37_to_load_all_clients_of_firm
|
||||
companies(:first_firm).clients_of_firm.each {|f| }
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue