I am trying to find a clever way to accommodate having a relationship between two models where there is really no shared identifier. The way that the relationship would work instead would be that it would pull the related model based on a date field of the original model.
For example, if you had a model that looked something like this:
Model A
id
name
created_at
updated_at
And another model:
Model B
id
name
published_at
How could I use ModelA.updated_at field to find the appropriate ModelB. The actual logic would grab the ModelA.updated_at and find the first model in set of ModelB where updated_at is greater than published_at.
The two things that I can think of off the top of my head are to just use a messy accessor of some kind to pull that data rather than a relationship. I am also thinking that I could use model events and set an actual concrete ID on ModelA every time a model is created or saved.
It's kind of hard to explain, but essentially ModelA is something a user would create. ModelB is something a user doesn't actually set on the Model via the UI, but implicitly we need to attach it so we can query off of that hidden relationship.
I don't think I did a very good job explaining that, but let me know if that makes sense.
So it isn't a user relationship at all. I suppose I can throw the actual concepts in there to clarify (was initially thinking this would muddy it up).
So I have 2 models:
Build (a user-created set of selections)
Patch (a admin-managed concept)
When a new patch is created, I want all new builds created after the time the patch is added to reference the new patch. All old builds should reference the patch that matches the time it was created. User's don't select a patch because it isn't a scenario where multiple current versions of the software are running at once (basically always points to head). We still want to know when old data is out there so we can mark it as older (by comparing the updated_at with the published_at of the Patch model.
That does make more sense. Not the best architecture but doable.
Then you do not need a relationship, you just need to create an observer that will run for example when new Patch is created. Or you can schedule a command.