Skip to content

Commit

Permalink
Belongs-to fallback should gracefully fail when unable to find associ…
Browse files Browse the repository at this point in the history
…ation data
  • Loading branch information
cbeer committed Oct 17, 2016
1 parent 6ecd254 commit 0794319
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/active_fedora/associations/belongs_to_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def find_target
if @full_result # side-effect from #id_reader
ActiveFedora::SolrService.reify_solr_results([@full_result]).first
else
ActiveFedora::Base.find(id_reader, cast: true)
begin
ActiveFedora::Base.find(id_reader, cast: true)
rescue ActiveFedora::ObjectNotFoundError
nil
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions spec/integration/belongs_to_association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class SpecialInheritedBook < Book
book[:library_id] = library.id
book.library_id.should == library.id
end

it "safely handles invalid data" do
book[:library_id] = 'bad identifier'
book.library.should be_nil
end
end

describe "getting the id property" do
Expand Down

0 comments on commit 0794319

Please sign in to comment.