Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Active Resource

  test_belongs_to_accessor_should_return_nil_when_the_does_not_has_an_associated_resource
  • Loading branch information...
commit 51e9cc0754997783930dba32f078e26317911138 1 parent 55156b1
@gramos authored
View
4 activeresource/lib/active_resource/associations.rb
@@ -61,7 +61,9 @@ def belongs_to(resource, opts = {})
define_method(klass_name) do
if !instance_variable_defined?("@#{resource}") ||
instance_variable_get("@#{resource}").nil?
- instance_variable_set("@#{resource}", h[:klass].find(send h[:association_col]))
+ association_col = send h[:association_col]
+ return nil if association_col.nil?
+ instance_variable_set("@#{resource}", h[:klass].find(association_col))
end
return instance_variable_get("@#{resource}")
end
View
4 activeresource/test/cases/associations_test.rb
@@ -18,6 +18,7 @@ class Project < ActiveResource::Base
@project_managers = [@project_manager]
ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/projects/.xml", {}, @project.to_xml(:root => 'project')
mock.get "/projects/1.xml", {}, @project.to_xml(:root => 'project')
mock.get "/projects/2.xml", {}, @other_project.to_xml(:root => 'project')
mock.get "/project_managers/5.xml", {}, @project_manager.to_xml(:root => 'project_manager')
@@ -69,5 +70,8 @@ def test_belongs_to_accessor_should_return_the_associated_project
assert_equal @project, @project_manager.project
end
+ def test_belongs_to_accessor_should_return_nil_when_the_does_not_has_an_associated_resource
+ assert_nil @other_project_manager.project
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.