ActiveRecordDistinctOn adds support for DISTINCT ON
queries to ActiveRecord. At the time of this writing, PostgreSQL is the only database which supports this syntax; however, this gem has been written with database independence in mind so that if another Arel visitor adds support for Arel::Nodes::DistinctOn
in the future, it should work seamlessly.
Add this line to your application's Gemfile:
gem 'active_record_distinct_on'
And then execute:
$ bundle
Or install it yourself as:
$ gem install active_record_distinct_on
This gem defines ActiveRecord::Base.distinct_on
and ActiveRecord::Relation#distinct_on
. Both accept a variable number of column arguments.
For example:
Dog.distinct_on(:name, :owner_id)
Generates SQL like:
SELECT DISTINCT ON ( "dogs"."name", "dogs.owner_id" ) "dogs".* FROM "dogs"
Note: For applications using ActiveRecord à la carte (without the rails
gem), none of the methods above will be defined until ActiveRecordDistinctOn.install
is manually called.
The development dependencies of this gem are managed using Bundler.
After checking out the repo, run bundle install
to install dependencies. Then, run bundle exec rake test:all
to run the tests. You can also run bundle exec rake console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to RubyGems.
Bug reports and pull requests are welcome on GitHub.
The gem is available as open source under the terms of the MIT License.