active_fedora icon indicating copy to clipboard operation
active_fedora copied to clipboard

#to_param returns nil on an unpersisted object

Open jcoyne opened this issue 8 years ago • 3 comments

The implementation in ActiveModel checks for persisted? and I don't think we need to do that: https://github.com/rails/rails/blob/master/activemodel/lib/active_model/conversion.rb#L81

jcoyne avatar May 25 '17 15:05 jcoyne

The ActiveModel logic is that the id is not authoritative unless the object has been persisted, right? Which makes sense, for ActiveRecord. Out situation is more complicated:

  • if we mint our own IDs, it happens before persistence, so we can respond accurately, but
  • if we are relying on Fedora persistence to auto-mint an ID, then we cannot respond accurately. We are in the same position as ActiveRecord then.

Am I understanding correctly?

atz avatar May 31 '17 22:05 atz

@atz Even more complicated than that, I think? There are (at least) two mechanisms for setting an ID on an AF::Base object:

  1. Pass it in when constructed, via the id: 'whatever' attribute. In this case, the ID is held in an in-memory LdpResource object (based on an AT::Resource, IIRC). In practice, though, I've only seen this done in tests so that we can predict IDs in advance.
  2. The pattern used by AF::Noid, which may pre-date the ability to pass an ID into the constructor, is to override AF's #assign_id method. This method is not called until #save is called. So ID minting, a la AF::Noid, happens at the point of persistence.

mjgiarlo avatar Jun 01 '17 15:06 mjgiarlo

Mostly this would just be for testing, so that we could use unpersisted models in tests. Maybe we should just use mock_model instead in this case. That mocks persisted? to return true.

jcoyne avatar Jun 01 '17 15:06 jcoyne