All Projects → markets → Unscoped_associations

markets / Unscoped_associations

Licence: mit
🔦 Skip the default_scope in your associations (ActiveRecord)

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Unscoped associations

Ransack
Object-based searching.
Stars: ✭ 5,020 (+9371.7%)
Mutual labels:  activerecord, ruby-gem
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-60.38%)
Mutual labels:  ruby-gem, activerecord
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (-49.06%)
Mutual labels:  ruby-gem, activerecord
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (-47.17%)
Mutual labels:  ruby-gem, activerecord
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (+418.87%)
Mutual labels:  activerecord, ruby-gem
knockoff
A gem for easily using read replicas.
Stars: ✭ 19 (-64.15%)
Mutual labels:  ruby-gem, activerecord
rails cursor pagination
Add cursor pagination to your ActiveRecord backed application
Stars: ✭ 21 (-60.38%)
Mutual labels:  ruby-gem, activerecord
Postgresql cursor
ActiveRecord PostgreSQL Adapter extension for using a cursor to return a large result set
Stars: ✭ 384 (+624.53%)
Mutual labels:  activerecord, ruby-gem
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+1115.09%)
Mutual labels:  activerecord, ruby-gem
Octopoller.rb
A micro gem for polling and retrying. Perfect for making repeating requests.
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Ar Uuid
Override migration methods to support UUID columns without having to be explicit about it.
Stars: ✭ 41 (-22.64%)
Mutual labels:  activerecord
Lockbox
Modern encryption for Ruby and Rails
Stars: ✭ 905 (+1607.55%)
Mutual labels:  activerecord
Jekyll Pug
Jekyll Plugin That Allows You To Use Pug
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Fabrication
This project has moved to GitLab! Please check there for the latest updates.
Stars: ✭ 1,017 (+1818.87%)
Mutual labels:  activerecord
Zache
Zero-footprint Ruby In-Memory Thread-Safe Cache
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Minimal Mistakes
📐 Jekyll theme for building a personal site, blog, project documentation, or portfolio.
Stars: ✭ 8,967 (+16818.87%)
Mutual labels:  ruby-gem
Activerecord Sqlserver Adapter
SQL Server Adapter For Rails
Stars: ✭ 910 (+1616.98%)
Mutual labels:  activerecord
Hold please
📞 Disable ActiveRecord callbacks in Rails for great justice!
Stars: ✭ 20 (-62.26%)
Mutual labels:  activerecord
Logidze
Database changes log for Rails
Stars: ✭ 1,060 (+1900%)
Mutual labels:  activerecord
Power trace
Buff exception backtrace with local variables, passed in arguments and instance variables!
Stars: ✭ 48 (-9.43%)
Mutual labels:  ruby-gem

Unscoped Associations

Gem Version Build Status

Have you ever needed to skip the default_scope when fetching objects through associations methods (for some strange reasons)? Do it easily with this Active Record extension!

Supported associations:

  • :belongs_to
  • :has_one
  • :has_many

Officially supported (tested) Active Record versions: 3.2, 4.0, 4.1, 4.2 and 5.0.

Installation

Add this line to your Gemfile:

gem 'unscoped_associations'

Or install the gem manually:

gem install unscoped_associations

Usage

Basic usage example:

class User < ActiveRecord::Base
  has_many :comments
  has_many :all_comments, class_name: 'Comment', unscoped: true

  default_scope { where(active: true) }
end

class Comment < ActiveRecord::Base
  belongs_to :user, unscoped: true

  default_scope { where(public: true) }
end

From now on, you get:

  • @user.comments: returns all public comments
  • @user.all_comments: returns all comments skipping the default_scope
  • @comment.user: returns the user without taking account the 'active' flag

Status

This project was originally thought and built for a Rails 3.2 application.

Rails 4 introduces some updates regarding associations. For example, since Rails 4 (AR 4 to be precise), you are able to customize associations using a scope block (overriding conditions), so you can skip the default_scope conditions by:

class User < ActiveRecord::Base
  has_many :all_comments, -> { where(public: [true, false]) }, class_name: 'Comment'
end

Since Rails 4.1, you can also override the default conditions using the unscope method:

class User < ActiveRecord::Base
  has_many :all_comments, -> { unscope(where: :public) }, class_name: 'Comment'
end

Anyway, you can continue using unscoped_associations, could be useful in certain situations, for example, if you prefer to bypass the entire default_scope, given a scope with multiple conditions, like:

default_scope { where(public: true, deleted_at: nil).order(:updated_at) }

Rails 5.0 is also supported as a migration path to facilitate upgrades.

Notes

  • Under the hood, Unscoped Associations relies on the unscoped method (from AR). So, chaining unscoped associations with other AR query methods won't work. E.g.: @user.all_comments.count will load comments with the defaul_scope applied. In this case, @user.all_comments.to_a.count should work.
  • Unscoped Associations doesn't touch the preloading layer, so includes, joins, ... in combination with an unscoped association can cause N+1 problems.

Contributing

Any kind of fixes, both code and docs, or enhancements are really welcome!

To contribute, just fork the repo, hack on it and send a pull request. Don't forget to add specs for behaviour changes and run the tests by:

bundle exec rspec
bundle exec appraisal rspec # run against all supported AR versions

License

Copyright (c) Marc Anguera. Unscoped Associations is released under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].