Skip to content

Commit

Permalink
Merge pull request #44 from krokodily/has_one_nil_fix
Browse files Browse the repository at this point in the history
Fixed has_one association when it's nil.
  • Loading branch information
andyjeffries committed Jun 21, 2016
2 parents 36544d1 + c4d3ae5 commit e32f772
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/flexirest/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def has_one(key, klass = nil)
@_associations ||= {}
@_associations[key] = klass
define_method(key) do
return nil if _attributes[key].nil?

if _attributes[key].is_a?(klass)
return _attributes[key]
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class DeepNestedHasManyExample < Flexirest::Base
describe "Has One Associations" do
let(:subject) {AssociationExampleBase.new}

it "should return nil if it's nil" do
subject.child = nil
expect(subject.child).to be_nil
end

it "should return a list of the association class" do
subject.child = {test: "foo"}
expect(subject.child).to be_an(AssociationExampleOther)
Expand Down

0 comments on commit e32f772

Please sign in to comment.