lotus/model — pull request #56 by heruku (2014-08-28 22:24:11+00:00)
Title: Associations

#35
This is not a final/complete implementation of this feature - the purpose of this pull request is to facilitate discussion around the implementation details of associations.

I want to highlight some issues that I came across with the implementation:

* Visibility - The components of the framework don't have complete knowledge of each other, which is a good design decision, but some parts of this feature feel awkward for this reason. Should the mapping/collection know about it's parent mapper? Should query objects know about the mapping/collection they correspond to? I feel like some parts of this implementation could be cleaner that way.

* Performance - Since this is just a draft, I implemented a naive N+1 query, but I would love to hear ideas on optimizing it.

* Repository's public interface - To fetch associations, I see three options:
* Reach into the private query object of the target association. This is what happens in this implementation and it's quite ugly.
* Dynamically adding a finder to the target repository
* Add a method on all repositories to fetch records based on a field(e.g. `repository.find_by(:article_id, [1,2,3])`) - essentially exposing the `where` method.

I don't think there's a nice, clean way to do this, we just have to choose the lesser of all the evils.

* `collection#association` api. What has to be specified? What can we infer? How do we distinguish between `belongs_to` and `has_one` ?