aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Fix counter_cache double increment bugTom Kadwill2016-04-282-0/+3
|
* Allow symbols using "dot notation" to be passed to whereSean Griffin2016-04-121-2/+2
| | | | | | | | | | | | | | | | | In 04ac5655be91f49cd4dfe2838df96213502fb274 I assumed that we would never want to pass the "table_name.column_name" form to where with a symbol. However, in Ruby 2.2 and later, you can quote symbols using the new hash syntax, so it's a semi-reasonable thing to do if we want to support the dot notation (which I'd rather deprecate, but that would be too painful of a migration). Instead we've changed the definition of "this is a table name with a dot" to when the value associated is a hash. It would make very little sense to write `where("table_name.column_name": { foo: :bar })` in any scenario (other than equality for a JSON column which we don't support through `where` in this way). Close #24514.
* Fix undefined method `owners' for NullPreloader:ClassLadislav Smola2016-04-061-0/+1
| | | | | | | | | | | | | | * Fix undefined method `owners' for NullPreloader:Class Fixing undefined method `owners' for ActiveRecord::Associations::Preloader::NullPreloader:Class * Use Ruby 1.9 hash format Use Ruby 1.9 hash format #24192 [Rafael Mendonça França + Ladislav Smola]
* Ensure associations still work when the table name contains a dotSean Griffin2016-03-311-2/+2
| | | | | | | | | | | | | | | This issue occured because associations now call `where` directly, and a dot in the key name for `where` means nested tables. For this fix, we now pass the table name as a symbol, and do not attempt to expand symbols containing a dot. This is a temporary fix. I do not think we should support table names containing a dot, as it has a special meaning in most backends, as well as most APIs that involve table names. This commit does not include a test, as I am going to deprecate table names containing dots in the following commit. Fixes #24367
* Merge pull request #22518 from ↵Arthur Nogueira Neves2016-03-181-1/+1
|\ | | | | | | | | RochesterinNYC/better-error-message-for-includes-relations-missing Improve error message for missing relations for includes and eager_load
| * Improve clarity of error message for missing includes and eager_loadJames Wen2016-01-281-1/+1
| | | | | | | | relations
* | start hiding the `scope_chain` data structureAaron Patterson2016-03-041-1/+1
| | | | | | | | | | Introduce a predicate method that doesn't need to build a scope chain, but also hides the data structure used for representing the scope chain.
* | don't treat all associations with extensions as instance dependent.Yves Senn2016-03-031-1/+5
| | | | | | | | | | | | | | | | | | | | Closes #23934. This is a forward port of ac832a43b4d026dbad28fed196d2de69ec9928ac Previously the scope of all associations with extensions were wrapped in an instance dependent proc. This made it impossible to preload such associations.
* | Merge pull request #18766 from yasyf/issue_17864Sean Griffin2016-02-291-8/+15
|\ \ | | | | | | | | | | | | Honour joining model order in `has_many :through` associations when eager loading
| * | Honour the order of the joining model in a `has_many :through`Yasyf Mohamedali2015-03-022-9/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | association when eager loading. Previously, eager loading a `has_many :through` association with no defined order would return the records in the natural order of the database. Now, these records will be returned in the order that the joining record is returned, in case there is a defined order there.
* | | Merge pull request #23794 from matthewd/mutation-safe-to_aMatthew Draper2016-02-212-1/+5
|\ \ \ | | | | | | | | | | | | Mutating the result of Relation#to_a should not affect the relation
| * | | Mutating the result of Relation#to_a should not affect the relationMatthew Draper2016-02-212-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | Clarifying this separation and enforcing relation immutability is the culmination of the previous efforts to remove the mutator method delegations.
* | | | eliminate warnings about multiple primary keys on habtm join tablesAaron Patterson2016-02-191-0/+3
|/ / / | | | | | | | | | | | | | | | | | | | | | habtm join tables commonly have two id columns and it's OK to make those two id columns a primary key. This commit eliminates the warnings for join tables that have this setup. ManageIQ/manageiq#6713
* | | Merge pull request #18109 from k0kubun/unscoped-joinsSean Griffin2016-02-111-6/+12
|\ \ \ | | | | | | | | | | | | | | | | | | | | Allow `joins` to be unscoped Fixes #13775
| * | | Allow `joins` to be unscopedTakashi Kokubun2016-01-311-6/+12
| | |/ | |/|
* | | rename to 'second_to_last' and 'third_to_last'Brian Christian2016-02-102-8/+8
| | |
* | | allow Array.penultimate and Array.antepenultiate access methodsBrian Christian2016-02-092-0/+18
| | |
* | | Merge pull request #23457 from matthewd/arel-attributeMatthew Draper2016-02-042-2/+2
|\ \ \ | | | | | | | | Defer Arel attribute lookup to the model class
| * | | Defer Arel attribute lookup to the model classMatthew Draper2016-02-042-2/+2
| |/ / | | | | | | | | | | | | This still isn't as separated as I'd like, but it at least moves most of the burden of alias mapping in one place.
* | | Merge pull request #20997 from himesh-r/issue-20995Arthur Neves2016-02-021-1/+4
|\ \ \ | |/ / |/| | | | | | | | Changed id-writer to save join table records based on association primary key #20995.
| * | Changed id-writer to save join table records based on association primary ↵Himesh2016-02-021-1/+4
| | | | | | | | | | | | | | | | | | key #20995 Changed id-writer to save join table records based on association primary key
* | | Add missing source_type if provided on hmt which belongs to an sti recordVipul A M2016-01-241-0/+5
| | | | | | | | | | | | Fixes #23209
* | | Define `:foreign_type` as a valid option in `SingularAssociation`yui-knk2016-01-193-3/+3
| | | | | | | | | | | | | | | `:foreign_type` is a valid option for `belongs_to` and `has_one` so remove this to `SingularAssociation`.
* | | `substitute_at` is no longer usedRyuta Kamizono2016-01-141-2/+1
| | | | | | | | | | | | Arel handles substitution for bind parameters by now.
* | | Revert "Merge pull request #22486 from ↵Yves Senn2015-12-211-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | methyl/fix-includes-for-groupped-association" This reverts commit 537ac7d6ade61e95f2b70685ff2236b7de965bab, reversing changes made to 9c9c54abe08d86967efd3dcac1d65158a0ff74ea. Reason: The way we preload associations will change the meaning of GROUP BY operations. This is illustrated in the SQL generated by the added test (failing on PG): Association Load: D, [2015-12-21T12:26:07.169920 #26969] DEBUG -- : Post Load (0.7ms) SELECT "posts".* FROM "posts" LEFT JOIN comments ON comments.post_id = posts.id WHERE "posts"."author_id" = $1 GROUP BY posts.id ORDER BY SUM(comments.tags_count) [["author_id", 1]] Preload: D, [2015-12-21T12:26:07.128305 #26969] DEBUG -- : Post Load (1.3ms) SELECT "posts".* FROM "posts" LEFT JOIN comments ON comments.post_id = posts.id WHERE "posts"."author_id" IN (1, 2, 3) GROUP BY posts.id ORDER BY SUM(comments.tags_count)
* | | Merge pull request #22486 from methyl/fix-includes-for-groupped-associationYves Senn2015-12-211-1/+5
|\ \ \ | | | | | | | | | | | | | | | | | | | | | | | | Pass group values when including association Conflicts: activerecord/CHANGELOG.md
| * | | Pass SQL group by values when including scoped associationLucjan Suski2015-12-151-1/+5
| | | | | | | | | | | | | | | | | | | | Fixes problem when added `group()` in association scope was lost in eager loaded association.
* | | | Merge pull request #19423 from ↵Aaron Patterson2015-12-181-2/+7
|\ \ \ \ | |/ / / |/| | | | | | | | | | | yuroyoro/fix_performance_regression_of_preloading_has_many_through_relation Fix #12537 performance regression when preloading has_many_through association
| * | | Read already loaded association records from association.targetTomohito Ozaki2015-04-171-2/+7
| | | | | | | | | | | | | | | | | | | | For performance, Avoid instantiate CollectionProxy. Fixes #12537
* | | | Make sure we touch all the parents when touch_later.Arthur Neves2015-12-061-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The problem was that when saving an object, we would call touch_later on the parent which wont be saved immediteally, and it wont call any callbacks. That was working one level up because we were calling touch, during the touch_later commit phase. However that still didnt solve the problem when you have a 3+ levels of parents to be touched, as calling touch would affect the parent, but it would be too late to run callbacks on its grand-parent. The solution for this, is instead, call touch_later upwards when the first touch_later is called. So we make sure all the timestamps are updated without relying on callbacks. This also removed the hard dependency BelongsTo builder had with the TouchLater module. So we can still have the old behaviour if TouchLater module is not included. [fixes 5f5e6d924973003c105feb711cefdb726f312768] [related #19324]
* | | | Bugfix collection association #create method …Bogdan Gusiev2015-11-231-4/+8
| | | | | | | | | | | | | | | | | | | | When same association is loaded in the model creation callback The new object is inserted into association twice
* | | | Except keys of `build_record`'s argument from `create_scope` in ↵yui-knk2015-11-161-3/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | initialize_attributes If argument of `build_record` has key and value which is same as default value of database, we should also except the key from `create_scope` in `initialize_attributes`. Because at first `build_record` initialize record object with argument of `build_record`, then assign attributes derived from Association's scope. In this case `record.changed` does not include the key, which value is same as default value of database, so we should add the key to except list. Fix #21893.
* | | | Update belongs_to.rbEhsan Yousefi2015-11-081-2/+1
| | | | | | | | | | | | There is no need to to assign reflection name to a variable, because it's called once.
* | | | docs, `ActiveRecord::JoinDependency` is not part of the public API.Yves Senn2015-11-071-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | [ci skip] While `JoinDependency` and `JoinDependency::Aliases` were nodoced, the inner `Table` class made them appear in the API.
* | | | Merge pull request #12071 from Crunch09/outer_joinsSean Griffin2015-10-301-2/+15
|\ \ \ \ | | | | | | | | | | | | | | | added ActiveRecord::Relation#outer_joins
| * | | | added ActiveRecord::Relation#left_outer_joinsFlorian Thomas2015-05-191-2/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Example: User.left_outer_joins(:posts) => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
* | | | | Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`Sean Griffin2015-10-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit, if `ActiveRecord::Base.belongs_to_required_by_default` is set to `true`, then creating a record through `has_and_belongs_to_many` fails with the cryptic error message `Left side must exist`. This is because `inverse_of` isn't working properly in this case, presumably since we're doing trickery with anonymous classes in the middle. Rather than following this rabbit hole to try and get `inverse_of` to work in a case that we know is not publicly supported, we can just turn off this validation to match the behavior of 4.2 and earlier.
* | | | | Never pass `nil` to `order`Sean Griffin2015-10-294-16/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is part of a refactoring to make it easier to allow `order` to use sanitize like just about everything else on relation. The deleted test doesn't give any reasoning as to why passing `nil` to `order` needs to be supported, and it's rather nonsensical. I can almost see allowing an empty string being passed (though I'm tempted to just disallow it...)
* | | | | Revert "Revert "Merge pull request #22026 from ↵Yves Senn2015-10-291-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | akihiro17/fix-preload-association"" This reverts commit 5243946017d09afff4d70d273b0fcdfd41a4b22a. This fixes an issue with the build where tests would fail on mysql and postgresql due to different ordering.
* | | | | Revert "Merge pull request #22026 from akihiro17/fix-preload-association"Sean Griffin2015-10-291-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 6dc6a0b17cfaf7cb6aa2b1c163b6ca141b538a8e, reversing changes made to ec94f00ba3cf250eb54fc5b7a5e3ed4b90164f34. This pull request broke the build.
* | | | | Set `scope.reordering_value` to `true` if :reordering values are specifiedakihiro172015-10-301-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We should call `scope.order!` and set `scope.reordering_value` to `true` if :reordering values are specified Fixes #21886
* | | | | Merge pull request #18383 from ↵Rafael Mendonça França2015-10-271-1/+2
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | scambra/habtm-with-where-includes-16032-for-master Includes HABTM returns correct size now
| * | | | | Includes HABTM returns correct size now. It's caused by the join dependencySergio Cambra2015-01-071-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | only instantiates one HABTM object because the join table hasn't a primary key. Updated commit from @bigxiang commit dbaa837 Fixes #16032. Examples: before: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 1 after: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 3
* | | | | | [ci skip] Add doc for preloader_forschneems2015-10-261-0/+4
| | | | | |
* | | | | | [ci skip] Clarify doc for preloaders_for_oneschneems2015-10-261-0/+5
| | | | | |
* | | | | | [ci skip] Add doc to preloaders_onschneems2015-10-261-0/+1
| | | | | |
* | | | | | Merge pull request #19686 from tsun1215/index_errorsSean Griffin2015-10-261-1/+1
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Errors can be indexed with nested attributes Close #8638
| * | | | | | Errors can be indexed with nested attributesMichael Probber2015-04-171-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun]
* | | | | | | [ci skip] Fix method documentation locationschneems2015-10-261-3/+2
| | | | | | |
* | | | | | | Merge pull request #21918 from bogdan/refactor-preloaderSean Griffin2015-10-221-42/+30
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Refactored association preloader for performance