active_fedora icon indicating copy to clipboard operation
active_fedora copied to clipboard

CollectionProxy#count on unsaved parent returns all objects in scope.

Open jcoyne opened this issue 8 years ago • 4 comments

This problem only occurs when class_name: 'ActiveFedora::Base' is specified.

class Library < ActiveFedora::Base
  has_many :books,  predicate: ::RDF::Vocab::DC.isPartOf,  class_name: 'ActiveFedora::Base'
end

class Book < ActiveFedora::Base
  belongs_to :library, predicate: ::RDF::Vocab::DC.isPartOf
end

Book.create!
Book.create!
Library.new.books.count
#=>2

jcoyne avatar May 03 '17 15:05 jcoyne

@jcoyne: Is it acceptable to say that ActiveFedora::Base is not a valid class_name for the association and raise when it is received?

atz avatar Jul 06 '17 20:07 atz

I don't think so. We may want to accept several types of classes that extend AF::Base and this has been the way to do this.

Consider the following:

class Novel < ActiveFedora::Base; end
class Atlas < ActiveFedora::Base; end

library = Library.create
library.books = [Novel.create, Atlas.create]

jcoyne avatar Jul 06 '17 21:07 jcoyne

Yeah, rejecting Base would require the implementer to have some other nominal class (probably extending the same).

class MyApp::Base < ActiveFedora::Base; end
class Novel < MyApp::Base; end
class Atlas < MyApp::Base; end

library = Library.create
library.books = [Novel.create, Atlas.create]

I'm not strongly enamored of it, but I'm not entirely against it since it seems useful in other ways.

atz avatar Jul 06 '17 22:07 atz

@jcoyne: there really isn't a comparably simple ActiveRecord equivalent, though, right? You can't just declare has_many ActiveRecord::Base and then expect to backfill polymorphic associations to anything later, right? There are sound logical reasons to impose more constraint here.

atz avatar Jul 06 '17 22:07 atz