2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-06-27 11:17:44 -04:00
|
|
|
class Car < ActiveRecord::Base
|
2010-08-12 21:37:48 -04:00
|
|
|
has_many :bulbs
|
Fix ActiveRecord::Relation#unscope
I'm pretty confused about the addition of this method. The documentation
says that it was intended to allow the removal of values from the
default scope (in contrast to #except). However it behaves exactly the
same as except: https://gist.github.com/jonleighton/7537008 (other than
having a slightly enhanced syntax).
The removal of the default scope is allowed by
94924dc32baf78f13e289172534c2e71c9c8cade, which was not a change we
could make until 4.1 due to the need to deprecate things. However after
that change #unscope still gives us nothing that #except doesn't already
give us.
However there *is* a desire to be able to unscope stuff in a way that
persists across merges, which would allow associations to be defined
which unscope stuff from the default scope of the associated model. E.g.
has_many :comments, -> { unscope where: :trashed }
So that's what this change implements. I've also corrected the
documentation. I removed the guide references to #except as I think
unscope really supercedes #except now.
While we're here, there's also a potential desire to be able to write
this:
has_many :comments, -> { unscoped }
However, it doesn't make sense and would not be straightforward to
implement. While with #unscope we're specifying exactly what we want to
be removed from the relation, with "unscoped" we're just saying that we
want it to not have some things which were added earlier on by the
default scope. However in the case of an association, we surely don't
want *all* conditions to be removed, otherwise the above would just
become "SELECT * FROM comments" with no foreign key constraint.
To make the above work, we'd have to somehow tag the relation values
which get added when evaluating the default scope in order to
differentiate them from other relation values. Which is way too much
complexity and therefore not worth it when most use cases can be
satisfied with unscope.
Closes #10643, #11061.
2013-11-20 16:51:35 -05:00
|
|
|
has_many :all_bulbs, -> { unscope where: :name }, class_name: "Bulb"
|
2016-08-06 12:26:20 -04:00
|
|
|
has_many :funky_bulbs, class_name: "FunkyBulb", dependent: :destroy
|
|
|
|
has_many :failed_bulbs, class_name: "FailedBulb", dependent: :destroy
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :foo_bulbs, -> { where(name: "foo") }, class_name: "Bulb"
|
2015-11-15 09:56:52 -05:00
|
|
|
has_many :awesome_bulbs, -> { awesome }, class_name: "Bulb"
|
2011-05-10 18:35:01 -04:00
|
|
|
|
2011-05-12 18:29:07 -04:00
|
|
|
has_one :bulb
|
|
|
|
|
2010-08-03 17:26:59 -04:00
|
|
|
has_many :tyres
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :engines, dependent: :destroy, inverse_of: :my_car
|
|
|
|
has_many :wheels, as: :wheelable, dependent: :destroy
|
2010-08-03 17:26:59 -04:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
has_many :price_estimates, as: :estimate_of
|
2015-11-21 04:58:17 -05:00
|
|
|
|
2012-03-21 18:18:18 -04:00
|
|
|
scope :incl_tyres, -> { includes(:tyres) }
|
|
|
|
scope :incl_engines, -> { includes(:engines) }
|
2010-08-03 17:26:59 -04:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
scope :order_using_new_style, -> { order("name asc") }
|
2018-06-09 04:30:00 -04:00
|
|
|
|
|
|
|
attribute :wheels_owned_at, :datetime, default: -> { Time.now }
|
2010-06-27 11:17:44 -04:00
|
|
|
end
|
2010-09-07 13:08:03 -04:00
|
|
|
|
|
|
|
class CoolCar < Car
|
2016-08-06 12:26:20 -04:00
|
|
|
default_scope { order("name desc") }
|
2010-09-07 13:08:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class FastCar < Car
|
2016-08-06 12:26:20 -04:00
|
|
|
default_scope { order("name desc") }
|
2010-09-07 13:08:03 -04:00
|
|
|
end
|