aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | | 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
| | |
* | | Updated documentation of CollectionProxy#clear [ci skip]Prathamesh Sonpatki2015-03-061-0/+3
|/ / | | | | | | | | | | | | | | - CollectionProxy#clear method calls delete_all so the SQL is directly run into the database. - So the updated_at column of the object on which its run is not updated. - Closes #17161
* | Preserve Array#take(n) behaviour of HasManyAssociationAkira Matsuda2015-02-282-5/+5
| |
* | Require `belongs_to` by default.Josef Šimánek2015-02-213-8/+26
| | | | | | | | Deprecate `required` option in favor of `optional` for belongs_to.
* | Merge branch 'rm-take' into 4-1-stableRafael Mendonça França2015-02-202-0/+14
| |
* | Merge pull request #16989 from Empact/reload-cache-clearRafael Mendonça França2015-02-201-6/+4
|\ \ | | | | | | | | | Isolate access to @associations_cache and @aggregations_cache to the Associations and Aggregations modules, respectively.
| * | Isolate access to @associations_cache and @aggregations cache to the ↵Ben Woosley2014-09-281-6/+4
| | | | | | | | | | | | | | | | | | | | | | | | Associations and Aggregations modules, respectively. This includes replacing the `association_cache` accessor with a more limited `association_cached?` accessor and making `clear_association_cache` and `clear_aggregation_cache` private.
* | | `type_cast_from_user` -> `cast`Sean Griffin2015-02-171-1/+1
| | |
* | | Optimize none? and one? relation query methods to use LIMIT and COUNT.Eugene Gilburg2015-02-121-2/+4
| | | | | | | | | | | | | | | | | | | | | | | | Use SQL COUNT and LIMIT 1 queries for none? and one? methods if no block or limit is given, instead of loading the entire collection to memory. The any? and many? methods already follow this behavior. [Eugene Gilburg & Rafael Mendonça França]
* | | Correct errors in counter cache updatingSean Griffin2015-02-032-3/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The cache name should be converted to a string when given, not compared as a symbol. This edge case is already adequately covered by our tests, but was masked by another issue where we were incorrectly updating the counter cache twice. When paired with a bug where we didn't update the counter cache because we couldn't find a match with the name, this made it look like everything was working fine. Fixes #10865.
* | | Merge branch 'master' into pr/18316Mingdong Luo2015-01-3112-96/+73
|\ \ \ | | | | | | | | | | | | | | | | Conflicts: activerecord/CHANGELOG.md
| * \ \ Merge pull request #18700 from nygrenh/better-required-messageAaron Patterson2015-01-311-1/+1
| |\ \ \ | | | | | | | | | | Provide a better error message on :required association
| | * | | Provide a better error message on :required associationHenrik Nygren2015-01-281-1/+1
| | | | | | | | | | | | | | | | | | | | Fixes #18696.
| * | | | Remove most type related predicates from `Column`Sean Griffin2015-01-301-2/+2
| |/ / / | | | | | | | | | | | | | | | | | | | | Remaining are `limit`, `precision`, `scale`, and `type` (the symbol version). These will remain on the column, since they mirror the options to the `column` method in the schema definition DSL
| * | | Remove Relation#bind_paramsSean Griffin2015-01-272-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `bound_attributes` is now used universally across the board, removing the need for the conversion layer. These changes are mostly mechanical, with the exception of the log subscriber. Additional, we had to implement `hash` on the attribute objects, so they could be used as a key for query caching.
| * | | Use an `Attribute` object to represent a bind valueSean Griffin2015-01-271-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The column is primarily used for type casting, which we're trying to separate from the idea of a column. Since what we really need is the combination of a name, type, and value, let's use the object that we already have to represent that concept, rather than this tuple. No consumers of the bind values have been changed, only the producers (outside of tests which care too much about internals). This is *finally* possible since the bind values are now produced from a reasonable number of lcoations.
| * | | Don't rely on the internal representation of join valuesSean Griffin2015-01-272-2/+6
| | | | | | | | | | | | | | | | | | | | I'm going to be extracting this logic into a clause class, things need to go through a method and not access the values hash directly.