Joiner
This gem, abstracted out from Thinking Sphinx, turns a bunch of association trees from the perspective of a single model and builds a bunch of OUTER JOINs that can be passed into ActiveRecord::Relation's join
method. You can also find out the generated table aliases for each join, in case you're referring to columns from those joins at some other point.
If this gem is used by anyone other than myself/Thinking Sphinx, I'll be surprised. My reason for pulling it out is so I can more cleanly support Rails' changing approaches to join generation (see v3.1-v4.0 compared to v4.1-v5.1 compared to v5.2).
Installation
It's a gem - so you can either install it yourself, or add it to the appropriate Gemfile or gemspec.
gem install joiner --version 0.6.0
Usage
First, create a join collection, based on an ActiveRecord model:
joiner = Joiner::Joins.new User
Then you can add joins for a given association path. For example, if User has many articles, and articles have many comments:
joiner.add_join_to [:articles]
joiner.add_join_to [:articles, :comments]
If you need the table/join alias for a given association path, just ask for it:
joiner.alias_for([:articles, :comments])
And once you've loaded up all the joins, you'll want something you can push out into ActiveRecord::Relation#joins
:
User.joins(joiner.join_values)
You can also check if a given association path will return potentially more than one record (thus perhaps requiring aggregation), or find out what the model at the end of the path is:
path = Joiner::Path.new(User, [:articles, :comments])
path.aggregate?
path.model
Contributing
Please note that this project now has a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Licence
Copyright (c) 2013-2020, Joiner is developed and maintained by Pat Allan, and is released under the open MIT Licence.