MostRelated
most_related
returns models that have the most associated models in common
Installation
Add this line to your application's Gemfile:
gem 'most_related'
And then execute:
$ bundle
Or install it yourself as:
$ gem install most_related
Usage
Post example
class Post < ActiveRecord::Base
:authors
:tags, as: :most_related_by_tags
:authors, :tags, as: :most_related_by_author_or_tag
has_many :author_posts
has_many :authors, through: :author_posts
end
class Author < ActiveRecord::Base
has_many :author_posts
end
class AuthorPosts < ActiveRecord::Base
belongs_to :author
belongs_to :post
end
To return the posts with the most authors in common with post
, in descending order:
post.
To return the posts with the most tags in common with post
, in descending order:
post.
To return the posts with the most authors and tags in common with post
, in descending order:
post.
The count of the associated models in common is accessible on each returned model
post.
post.
post.
Note multiple associations do not work with sqlite.
Because of the use of group
, pagination is not supported.
Contributing
- 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