Skip to content

Commit bf8536f

Browse files
committed
Merge pull request #68 from jassa/fix-belongs_to
Ensure belongs_to finder method sends a non-nil key
2 parents 6101794 + 59568ac commit bf8536f

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/active_resource/associations.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Builder
1414
# === Options
1515
# [:class_name]
1616
# Specify the class name of the association. This class name would
17-
# be used for resolving the association class.
17+
# be used for resolving the association class.
1818
#
1919
# ==== Example for [:class_name] - option
2020
# GET /posts/123.json delivers following response body:
@@ -48,7 +48,7 @@ def has_many(name, options = {})
4848
# === Options
4949
# [:class_name]
5050
# Specify the class name of the association. This class name would
51-
# be used for resolving the association class.
51+
# be used for resolving the association class.
5252
#
5353
# ==== Example for [:class_name] - option
5454
# GET /posts/1.json delivers following response body:
@@ -74,7 +74,7 @@ def has_one(name, options = {})
7474
end
7575

7676
# Specifies a one-to-one association with another class. This class should only be used
77-
# if this class contains the foreign key.
77+
# if this class contains the foreign key.
7878
#
7979
# Methods will be added for retrieval and query for a single associated object, for which
8080
# this object holds an id:
@@ -83,7 +83,7 @@ def has_one(name, options = {})
8383
# Returns the associated object. +nil+ is returned if the foreign key is +nil+.
8484
# Throws a ActiveResource::ResourceNotFound exception if the foreign key is not +nil+
8585
# and the resource is not found.
86-
#
86+
#
8787
# (+association+ is replaced with the symbol passed as the first argument, so
8888
# <tt>belongs_to :post</tt> would add among others <tt>post.nil?</tt>.
8989
#
@@ -130,8 +130,8 @@ def defines_belongs_to_finder_method(method_name, association_model, finder_key)
130130
instance_variable_get(ivar_name)
131131
elsif attributes.include?(method_name)
132132
attributes[method_name]
133-
else
134-
instance_variable_set(ivar_name, association_model.find(send(finder_key)))
133+
elsif association_id = send(finder_key)
134+
instance_variable_set(ivar_name, association_model.find(association_id))
135135
end
136136
end
137137
end
@@ -149,7 +149,7 @@ def defines_has_many_finder_method(method_name, association_model)
149149
end
150150
end
151151
end
152-
152+
153153
# Defines the has_one association
154154
def defines_has_one_finder_method(method_name, association_model)
155155
ivar_name = :"@#{method_name}"

test/cases/association_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,22 @@ def test_defines_belongs_to_finder_method_with_instance_variable_cache
5858
2.times{person.customer}
5959
assert person.instance_variable_defined?(:@customer)
6060
end
61+
62+
def test_belongs_to_with_finder_key
63+
Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id')
64+
65+
person = Person.new
66+
person.stubs(:customer_id).returns(1)
67+
Customer.expects(:find).with(1).once()
68+
person.customer
69+
end
70+
71+
def test_belongs_to_with_nil_finder_key
72+
Person.defines_belongs_to_finder_method(:customer, Customer, 'customer_id')
73+
74+
person = Person.new
75+
person.stubs(:customer_id).returns(nil)
76+
Customer.expects(:find).with(nil).never()
77+
person.customer
78+
end
6179
end

0 commit comments

Comments
 (0)