markets / unscoped_associations Goto Github PK
View Code? Open in Web Editor NEW:flashlight: Skip the default_scope in your associations (ActiveRecord)
License: MIT License
:flashlight: Skip the default_scope in your associations (ActiveRecord)
License: MIT License
Hi Marc,
Great work on the gem. Found a typo on your documentation though:
class User < ActiveRecord::Base
has_many :all_comments, -> { where public: [true, flase] }, class_name: 'Comment'
end
You meant "false", not "flase" right? :D
Adding unscoped to association seems to break inverse_of and caching of parent model instances.
class RedemptionOffer < ActiveRecord::Base
has_one :voucher_template, inverse_of: :redemption_offer, dependent: :destroy
acts_as_paranoid
end
class VoucherTemplate < ActiveRecord::Base
belongs_to :redemption_offer, inverse_of: :voucher_template#, unscoped: true
acts_as_paranoid
end
jruby-1.7.24 :033 > r = RedemptionOffer.new
jruby-1.7.24 :034 > r.build_voucher_template
jruby-1.7.24 :035 > r.voucher_template.redemption_offer.class
=> RedemptionOffer(columns)
After uncommenting the #unscoped: true
from VoucherTemplate model:
jruby-1.7.24 :033 > r = RedemptionOffer.new
jruby-1.7.24 :034 > r.build_voucher_template
jruby-1.7.24 :035 > r.voucher_template.redemption_offer.class
=> NilClass
Tested on Rails 3.2.21. I might try to fix that later if I'll find the time. Overall nice gem, 10/10 would use again
Hey Markets,
Maybe I am doing something wrong but it seems to not work with rail 3.1.8.
My question is simple. Is this part of code has to work with your gem?
class Article < ActiveRecord::Base
belongs_to :user, :unscoped => true
end
class User << ActiveRecord::Base
has_many :articles
default_scope where("users.invisible != ?",true)
end
On Rails console running:
Article.find(1).user
This will return nil if user.invisible = true
Any advice? I lost one day and can not solve this....
I am using rails 4.2.3 and unscoped_associations 0.6.5.
Although the gem works when fetching associated records, it doesn't seem to work when counting associated records.
I'm using different models than your example, but the behavior in your example would be something like this: user.all_comments.count
returns a smaller number than user.all_comments.to_a.count
. Per SQL debugging in the rails console, the default scope is not skipped when .count is added.
Hi guys,
I'm having the following problem:
I've a couple of classes structured like follows:
class FilmOccupation < ActiveRecord::Base
belongs_to :theater, unscoped: true #works great with nothing or unscoped: false
end
class Theater < ActiveRecord::Base
has_many :film_occupations # same with unscoped: true/false
default_scope { where(active: true) }
end
I'm on ruby 2.1, Rails 3.2.21, and launching the following command:
FilmOccupation.last.theater
or anything launching the belongs_to associations, with or without archived theaters i alway get a load of
[...]
Theater Load (0.4ms) SELECT "theaters".* FROM "theaters" WHERE "theaters"."id" IN (8)
Theater Load (0.6ms) SELECT "theaters".* FROM "theaters" WHERE "theaters"."id" IN (8)
Theater Load (0.6ms) SELECT "theaters".* FROM "theaters" WHERE "theaters"."id" IN (8)
[...]
ending with
SystemStackError: stack level too deep
Suggestions?
Just installed unscoped_associations and got this error when trying to start rails or run the console.
Didn't change any other code, just included the package in the Gemfile and installed.
/<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:7:in `block in included': undefined method `alias_method_chain' for #<Class:ActiveRecord::Base>
Did you mean? alias_method (NoMethodError)
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:6:in `instance_eval'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:6:in `included'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:61:in `include'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:61:in `block in <top (required)>'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:61:in `instance_eval'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:61:in `<top (required)>'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:81:in `require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:76:in `each'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:76:in `block in require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:65:in `each'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:65:in `require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/bundler-2.0.2/lib/bundler.rb:114:in `require'
from /<my home>/<my project>/config/application.rb:17:in `<top (required)>'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:92:in `require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:92:in `preload'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:153:in `serve'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:141:in `block in run'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `loop'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application.rb:135:in `run'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/gems/2.4.0/gems/spring-2.0.2/lib/spring/application/boot.rb:19:in `<top (required)>'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /<my home>/.rbenv/versions/2.4.3/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]
Rails 5.1.7
When loading polymorphic association with unscoped: true
, it breaks eager loading with includes
.
Updated to 0.6.3 and getting:
NoMethodError: undefined method 'polymorphic?' for #<ActiveRecord::Reflection::AssociationReflection:0x7062f004>
Using activerecord-3.2.17
Is it possible to add a respond_to? 'polymorphic?'
to that code?
My model contains belongs_to :my_parent, unscoped: true
We pass the association name as an argument to super
in add_unscoped_association
. Rails interprets this as a 'truthy' value and forces a reload of the model. In Rails 5 this raises a deprecation warning:
DEPRECATION WARNING: Passing an argument to force an association to reload is now deprecated and will be removed in Rails 5.1. Please call `reload` on the parent object instead.
A resource with accepts_nested_attributes_for :other_resource won't properly load that nested resource. If you try resource.build_other_resource, it's still empty.
Rails fails to load the gem and throws this error on startup.
Versions:
.rvm/gems/ruby-2.4.0/gems/bundler-1.16.1/lib/bundler/runtime.rb:84:in `rescue in block (2 levels) in require': There was an error while trying to load the gem 'unscoped_associations'. (Bundler::GemRequireError)
Gem Load Error is: undefined method `alias_method_chain' for #<Class:ActiveRecord::Base>
Did you mean? alias_method
Backtrace for gem load error is:
/home/xbits/.rvm/gems/ruby-2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:7:in `block in included'
/home/xbits/.rvm/gems/ruby-2.4.0/gems/unscoped_associations-0.6.5/lib/unscoped_associations.rb:6:in `instance_eval'
I got this warning after using usncoped: true
in a has_may
belongs_to association
:
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /home/rhl/myapp/config/application.rb:8) DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /home/rhl/myapp/config/application.rb:8) DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /home/rhl/myapp/config/application.rb:8)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super.
I'll see If I can submit a pull request for this.
class User
has_many :comments do
def today
where('created_at <= ?', Time.now.end_of_day)
end
end
end
raises
NoMethodError (undefined method `today' for #<ActiveRecord::Associations::CollectionProxy []>):
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.