aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | | | no more class methods for JoinDependency [ci skip]Gaurav Sharma2015-09-291-2/+1
| | | | | | | | | | | | | | | | | | | | | `ActiveRecord::Associations::JoinDependency` now it’s own class` and `ActiveRecord::Associations::ThroughAssociationScope` doesn’t exists
* | | | | | | Fix `ActiveRecord::RecordNotFound` error message with custom primary key [ci ↵amitkumarsuroliya2015-09-261-7/+7
| |/ / / / / |/| | | | | | | | | | | | | | | | | skip]
* | | | | | Clean up the implementation of AR::DirtySean Griffin2015-09-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This moves a bit more of the logic required for dirty checking into the attribute objects. I had hoped to remove the `with_value_from_database` stuff, but unfortunately just calling `dup` on the attribute objects isn't enough, since the values might contain deeply nested data structures. I think this can be cleaned up further. This makes most dirty checking become lazy, and reduces the number of object allocations and amount of CPU time when assigning a value. This opens the door (but doesn't quite finish) to improving the performance of writes to a place comparable to 4.1
* | | | | | Merge pull request #21550 from didacte/unscope-associationsSean Griffin2015-09-241-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | ActiveRecord: use association's `unscope` when preloading
| * | | | | | Include association's `unscope` when preloadingJimmy Bourassa2015-09-091-1/+1
| | | | | | |
* | | | | | | Merge branch 'master' of github.com:rails/docrailsVijay Dev2015-09-181-1/+1
|\ \ \ \ \ \ \
| * | | | | | | [ci skip] Fix module name of documentyui-knk2015-09-171-1/+1
| |/ / / / / /
* | | | | | | `restrict_with_error` message will now respect owner’s human name in any ↵Ronak Jangir2015-09-122-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | locale [kuboon & Ronak Jangir]
* | | | | | | Merge pull request #21486 from bogdan/refactor-has-many-counter-cacheEileen M. Uchitelle2015-09-092-61/+10
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | HasManyAssociation: moved half of counter cache code to reflection
| * | | | | | | HasManyAssociation: moved half of counter cache code to reflectionBogdan Gusiev2015-09-032-61/+10
| |/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Current implementation has a lot of utility methods that accept reflection call a lot of methods on it and exit. E.g. has_counter_cache?(reflection) It causes confusion and inability to cache result of the method even through it always returns the same result for the same reflection object. It can be done easier without access to the association context by moving code into reflection itself. e.g. reflection.has_counter_cache? Reflection is less complex object than association so moving code there automatically makes it simplier to understand.
* / / / / / / Added nodoc for `arel` method which returns object of private api. [ci skip]Ronak Jangir2015-09-091-1/+1
|/ / / / / /
* | | | | | Only nullify persisted has_one target associationsAgis-2015-08-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since after 87d1aba3c `dependent: :destroy` callbacks on has_one assocations run *after* destroy, it is possible that a nullification is attempted on an already destroyed target: class Car < ActiveRecord::Base has_one :engine, dependent: :nullify end class Engine < ActiveRecord::Base belongs_to :car, dependent: :destroy end > car = Car.create! > engine = Engine.create!(car: car) > engine.destroy! # => ActiveRecord::ActiveRecordError: cannot update a > destroyed record In the above case, `engine.destroy!` deletes `engine` and *then* triggers the deletion of `car`, which in turn triggers a nullification of `engine.car_id`. However, `engine` is already destroyed at that point. Fixes #21223.
* | | | | | Added docs for CollectionProxy#take [ci skip]Ronak Jangir2015-08-231-0/+25
| | | | | |
* | | | | | Correct error message in Standard American english and add a test case for ↵prakash2015-08-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | the same.
* | | | | | Merge pull request #19683 from tristang/require-option-for-counter-cacheRafael Mendonça França2015-08-131-1/+8
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | Require explicit counter_cache option for has_many
| * | | | | | Require explicit counter_cache option for has_manyTristan Gamilis2015-04-071-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously has_many associations assumed a counter_cache was to be used based on the presence of an appropriately named column. This is inconsistent, since the inverse belongs_to association will not make this assumption. See issues #19042 #8446. This commit checks for the presence of the counter_cache key in the options of either the has_many or belongs_to association as well as ensuring that the *_count column is present.
* | | | | | | Skip statement cache on through association readerRafael Mendonça França2015-08-123-12/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* | | | | | | use correct DB connection for generated HABTM tableMatt Hanlon2015-08-071-3/+7
| | | | | | |
* | | | | | | Use memoization for collection associations ids readerMehmet Emin İNAÇ2015-08-031-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #21082 remove extra space
* | | | | | | Fix misleading errors for has_one through relationsMehmet Emin İNAÇ2015-07-221-2/+10
| |/ / / / / |/| | | | |
* | | | | | Deprecate and rename the keys for association restrict_dependent_destroyRoque Pinel2015-07-202-2/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously `has_one` and `has_many` associations were using the `one` and `many` keys respectively. Both of these keys have special meaning in I18n (they are considered to be pluralizations) so by renaming them to `has_one` and `has_many` we make the messages more explicit and most importantly they don't clash with linguistical systems that need to validate translation keys (and their pluralizations). The `:'restrict_dependent_destroy.one'` key should be replaced with `:'restrict_dependent_destroy.has_one'`, and `:'restrict_dependent_destroy.many'` with `:'restrict_dependent_destroy.has_many'`. [Roque Pinel & Christopher Dell]
* | | | | | Add missing method name to exception descriptionMehmet Emin İNAÇ2015-07-201-1/+1
| | | | | |
* | | | | | Fix counter_cache for polymorphic associationsStefan Kanev2015-07-191-4/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Also removes a false positive test that depends on the fixed bug: At this time, counter_cache does not work with polymorphic relationships (which is a bug). The test was added to make sure that no StaleObjectError is raised when the car is destroyed. No such error is currently raised because the lock version is not incremented by appending a wheel to the car. Furthermore, `assert_difference` succeeds because `car.wheels.count` does not check the counter cache, but the collection size. The test will fail if it is replaced with `car.wheels_count || 0`.
* | | | | | [ci skip] Fix typo in #any? RDocAndrii Ponomarov2015-07-171-1/+1
| | | | | |
* | | | | | Deprecate force association reload by passing truePrem Sichanugrist2015-07-152-0/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is to simplify the association API, as you can call `reload` on the association proxy or the parent object to get the same result. For collection association, you can call `#reload` on association proxy to force a reload: @user.posts.reload # Instead of @user.posts(true) For singular association, you can call `#reload` on the parent object to clear its association cache then call the association method: @user.reload.profile # Instead of @user.profile(true) Passing a truthy argument to force association to reload will be removed in Rails 5.1.
* | | | | | Revert "Revert "Reduce allocations when running AR callbacks.""Guo Xiang Tan2015-07-161-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit bdc1d329d4eea823d07cf010064bd19c07099ff3. Before: Calculating ------------------------------------- 22.000 i/100ms ------------------------------------------------- 229.700 (± 0.4%) i/s - 1.166k Total Allocated Object: 9939 After: Calculating ------------------------------------- 24.000 i/100ms ------------------------------------------------- 246.443 (± 0.8%) i/s - 1.248k Total Allocated Object: 7939 ``` begin require 'bundler/inline' rescue LoadError => e $stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' raise e end gemfile(true) do source 'https://rubygems.org' # gem 'rails', github: 'rails/rails', ref: 'bdc1d329d4eea823d07cf010064bd19c07099ff3' gem 'rails', github: 'rails/rails', ref: 'd2876141d08341ec67cf6a11a073d1acfb920de7' gem 'arel', github: 'rails/arel' gem 'sqlite3' gem 'benchmark-ips' end require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection('sqlite3::memory:') ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.boolean :admin t.timestamps null: false end end class User < ActiveRecord::Base default_scope { where(admin: true) } end admin = true 1000.times do attributes = { name: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", email: "foobar@email.com", admin: admin } User.create!(attributes) admin = !admin end GC.disable Benchmark.ips(5, 3) do |x| x.report { User.all.to_a } end key = if RUBY_VERSION < '2.2' :total_allocated_object else :total_allocated_objects end before = GC.stat[key] User.all.to_a after = GC.stat[key] puts "Total Allocated Object: #{after - before}" ```
* | | | | | Correct through associations using scopesSean Griffin2015-06-302-6/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The changes introduced to through associations in c80487eb were quite interesting. Changing `relation.merge!(scope)` to `relation = relation.merge(scope)` should in theory never cause any changes in behavior. The subtle breakage led to a surprising conclusion. The old code wasn't doing anything! Since `merge!` calls `instance_exec` when given a proc, and most scopes will look something like `has_many :foos, -> { where(foo: :bar) }`, if we're not capturing the return value, it's a no-op. However, removing the `merge` causes `unscope` to break. While we're merging in the rest of the chain elsewhere, we were never merging in `unscope` values, causing a breakage on associations where a default scope was being unscoped in an association scope (yuk!). This is subtly related to #20722, since it appears we were previously relying on this mutability. Fixes #20721. Fixes #20727.
* | | | | | docs, nodoc `NullPreloader` and `AlreadyLoaded`.Yves Senn2015-06-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | These classes are part of Active Record Preloader, which is not part of the public API.
* | | | | | Merge pull request #20673 from aditya-kapoor/correct-preload-docYves Senn2015-06-231-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | [ci skip] correct for ActiveRecord::Associations::Preloader
| * | | | | | [ci skip] correct for ActiveRecord::Associations::Preloader:Aditya Kapoor2015-06-231-1/+2
| | | | | | |
* | | | | | | docs, add missing closing bracket. [ci skip]Yves Senn2015-06-231-1/+1
|/ / / / / /
* | | | | | Merge pull request #20552 from jamesdabbs/belongs-to-polymorphic-force-reloadYves Senn2015-06-231-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | Fix `undefined method uncached` for polymorphic belongs_to #20426
| * | | | | | Fix `undefined method uncached` for polymorphic belongs_to #20426James Dabbs2015-06-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Unitialized polymorphic `belongs_to` associations raise an error while attempting to reload, as they attempt to make an uncached reload, but don't have a klass to fetch uncachedly. In this case, `loaded?` should be `false` anyway.
* | | | | | | thrown ActiveRecord::AssociationTypeMismatch when assigning a wrong value ↵Diego Carrion2015-06-221-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | for a namespaced association fixes #20541
* | | | | | | raise ActiveModel::MissingAttributeError when trying to access a ↵Diego Carrion2015-06-161-1/+1
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | relationship without the foreign key attribute fixes regression reported on #20253 ActiveRecord::Base#[] was not used cause of 8b95420
* | / / / / [ci skip] fix the `collection.clear` guideRoque Pinel2015-06-021-7/+8
| |/ / / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Improve the guide about `has_many` `collection.clear` to indicate the behavior for each dependent strategy according to `collection.delete_all`. Based on #17179, I changed the `collection.delete` docs to also clarify the default strategy for each `hm` and `hm:t` associations. Fixes #20170.
* | | | | Rename association option :class to :anonymous_classAndrew White2015-04-212-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In 1f006c an option was added called :class to allow passing anonymous classes to association definitions. Since using :class instead of :class_name is a fairly common typo even amongst experienced developers this can result in hard to debug errors arising in raise_on_type_mismatch? To fix this we're renaming the option from :class to :anonymous_class as that is a more correct description of what the option is for. Since this was an internal, undocumented option there is no need for a deprecation. Fixes #19659
* | | | | Autosave existing records on HMT associations when the parent is newSean Griffin2015-04-181-6/+4
| |/ / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | To me it seems like this should only be the case if `autosave: true` is set on the association. However, when implemented that way, it caused issues with has many associations, where we have explicit tests stating that child records are updated when the parent is new, even if autosave is not set (presumably to update the parent id, but other changed attributes would be persisted as well). It's quirky, but at least we should be consistently quirky. This constitutes a minor but subtle change in behavior, and therefore should not be backported to 4.2 and earlier. Fixes #19782
* | | | Batch touch parent recordsArthur Neves2015-04-081-6/+7
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | [fixes #18606] Make belongs_to use touch over touch_later when running the callbacks. Add more tests and small method rename Thanks Jeremy for the feedback.
* | | Merge pull request #19647 from codeout/association_updateRafael Mendonça França2015-04-061-0/+2
|\ \ \ | | | | | | | | Fix a regression introduced by removing unnecessary db call when replacing
| * | | Fix a regression introduced by removing unnecessary db call when replacingShintaro Kojima2015-04-041-0/+2
| | | | | | | | | | | | | | | | When replacing a has_many association with the same one, there is nothing to do with database but a setter method should still return the substituted value for backward compatibility.
* | | | Merge pull request #19448 from tgxworld/fix_activesupport_callbacks_clash_on_runRafael Mendonça França2015-04-061-1/+1
|\ \ \ \ | |/ / / |/| | | Fix AS::Callbacks raising an error when `:run` callback is defined.
| * | | Revert "Reduce allocations when running AR callbacks."Guo Xiang Tan2015-03-221-1/+1
| | |/ | |/| | | | | | | This reverts commit 796cab45561fce268aa74e6587cdb9cae3bb243e.
* | | Freeze static arguments for gsubbrainopia2015-04-021-1/+1
| | |
* | | Prefer string patterns for gsubbrainopia2015-04-021-1/+1
|/ / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | https://github.com/ruby/ruby/pull/579 - there is a new optimization since ruby 2.2 Previously regexp patterns were faster (since a string was converted to regexp underneath anyway). But now string patterns are faster and better reflect the purpose. Benchmark.ips do |bm| bm.report('regexp') { 'this is ::a random string'.gsub(/::/, '/') } bm.report('string') { 'this is ::a random string'.gsub('::', '/') } bm.compare! end # string: 753724.4 i/s # regexp: 501443.1 i/s - 1.50x slower
* | Reuse the CollectionAssociation#reader proxy cache if the foreign key is ↵Ben Woosley2015-03-151-2/+2
| | | | | | | | | | | | | | | | | | | | present from the start. When a new record has the necessary information prior to save, we can avoid busting the cache. We could simply clear the @proxy on #reset or #reset_scope, but that would clear the cache more often than necessary.
* | Doc fix about association hierarchykeepcosmos2015-03-151-2/+2
| |
* | Isolate access to .default_scopes in ActiveRecord::Scoping::DefaultBen Woosley2015-03-122-4/+2
| | | | | | | | | | | | | | | | | | | | | | Instead use .scope_attributes? consistently in ActiveRecord to check whether there are attributes currently associated with the scope. Move the implementation of .scope_attributes? and .scope_attributes to ActiveRecord::Scoping because they don't particularly have to do specifically with Named scopes and their only dependency, in the case of .scope_attributes?, and only caller, in the case of .scope_attributes is contained in Scoping.
* | Merge pull request #19234 from sivsushruth/doc_fixKasper Timm Hansen2015-03-071-1/+1
|\ \ | | | | | | Doc fix [ci skip]
| * | Doc fix [ci skip]Sushruth Sivaramakrishnan2015-03-071-1/+1
| | |