diff --git a/lib/active_fedora/associations/belongs_to_association.rb b/lib/active_fedora/associations/belongs_to_association.rb index 4c895fb06..163fc637e 100644 --- a/lib/active_fedora/associations/belongs_to_association.rb +++ b/lib/active_fedora/associations/belongs_to_association.rb @@ -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 diff --git a/spec/integration/belongs_to_association_spec.rb b/spec/integration/belongs_to_association_spec.rb index 994d489ea..b3d6358e7 100644 --- a/spec/integration/belongs_to_association_spec.rb +++ b/spec/integration/belongs_to_association_spec.rb @@ -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