aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Don't call `scope.eager_loading?` when `reflection_scope.where_clause` is emptyRyuta Kamizono2017-09-021-10/+10
| | | | | If `reflection_scope.where_clause` is empty, `scope` isn't changed. So `scope.eager_loading?` is always false.
* Merge pull request #30403 from yhirano55/fix_broken_doc_for_active_recordRyuta Kamizono2017-08-267-7/+7
|\ | | | | Fix broken doc for Active Record [ci skip]
| * Fix broken doc for Active Record [ci skip]Yoshiyuki Hirano2017-08-257-7/+7
| |
* | Should work inverse association when eager loadingRyuta Kamizono2017-08-251-3/+3
|/ | | | | | | This regression was caused by caa178c1. The block for `set_inverse_instance` should also be passed to join dependency. Fixes #30402.
* Through scope should not be affected by scopingRyuta Kamizono2017-08-152-12/+2
| | | | | | Follow up of #29834. Fixes #30266.
* Merge pull request #27609 from kamipo/fix_association_primary_keyRafael França2017-08-141-4/+7
|\ | | | | Fix `reflection.association_primary_key` for `has_many` association
| * Fix `reflection.association_primary_key` for `has_many` associationsRyuta Kamizono2017-08-131-4/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | It is incorrect to treat `options[:primary_key]` as `association_primary_key` if `has_many` associations because the `:primary_key` means the column on the owner record, not on the association record. It will break `ids_reader` and `ids_writer`. ```ruby people(:david).essay_ids # => ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'essays.first_name' in 'field list': SELECT `essays`.first_name FROM `essays` WHERE `essays`.`writer_id` = 'David' ``` Fixes #14439.
* | Delegate to `Enumerable#find` for `CollectionProxy`Ryuta Kamizono2017-08-132-17/+14
|/ | | | | Since `Relation` includes `Enumerable`, it is enough to use `super` simply.
* Merge pull request #29720 from gaurish/ar_find_error_message_improvementRafael França2017-08-111-1/+3
|\ | | | | Return Not found Ids in ActiveRecord::NotFound
| * Return Not found Ids in ActiveRecord::NotFoundGaurish Sharma2017-07-291-1/+3
| | | | | | | | | | This builds on top of 15e2da656f41af0124f7577858536f3b65462ad5. now it also returns exact Ids which were not found which will be debugging simple.
* | Specify `table.name` only when `scope.table` and `table` are different (#29058)Ryuta Kamizono2017-08-111-4/+12
| | | | | | Fixes #29045.
* | Passing `klass` to `StatementCache.new`Ryuta Kamizono2017-08-042-12/+8
|/ | | | | | Actually `StatementCache#execute` is always passed the same klass that the owner klass of the connection when the statement cache is created. So passing `klass` to `StatementCache.new` will make more DRY.
* Remove single element array preprocessRyuta Kamizono2017-07-261-1/+1
| | | | | Since 213796f, array predicate handler supports making binds, so the preprocess is no longer needed.
* Remove useless `JoinInformation`Ryuta Kamizono2017-07-252-5/+3
| | | | | Since 213796f removed `binds`, `JoinInformation` only contain `joins`. So it is enough to return `joins` simply.
* Merge pull request #29765 from lugray/fix_counter_cacheRafael França2017-07-241-3/+1
|\ | | | | Fix `counter_cache` double increment
| * Add test for fixed `counter_cache` double incrementLisa Ugray2017-07-191-3/+1
| | | | | | | | | | | | | | | | | | | | | | When an `after_create` callback did `update_attributes` on a record with multiple `belongs_to` associations with counter caches, even numbered associations would have their counters double-incremented. Fixes to `ActiveModel::Dirty` in 020abad fixed this. This adds regression tests for this bug fixed incidentally in the other commit, which also removed the need for the workaround using @_after_create_counter_called.
* | Refactor Active Record to let Arel manage bind paramsSean Griffin2017-07-242-5/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A common source of bugs and code bloat within Active Record has been the need for us to maintain the list of bind values separately from the AST they're associated with. This makes any sort of AST manipulation incredibly difficult, as any time we want to potentially insert or remove an AST node, we need to traverse the entire tree to find where the associated bind parameters are. With this change, the bind parameters now live on the AST directly. Active Record does not need to know or care about them until the final AST traversal for SQL construction. Rather than returning just the SQL, the Arel collector will now return both the SQL and the bind parameters. At this point the connection adapter will have all the values that it had before. A bit of this code is janky and something I'd like to refactor later. In particular, I don't like how we're handling associations in the predicate builder, the special casing of `StatementCache::Substitute` in `QueryAttribute`, or generally how we're handling bind value replacement in the statement cache when prepared statements are disabled. This also mostly reverts #26378, as it moved all the code into a location that I wanted to delete. /cc @metaskills @yahonda, this change will affect the adapters Fixes #29766. Fixes #29804. Fixes #26541. Close #28539. Close #24769. Close #26468. Close #26202. There are probably other issues/PRs that can be closed because of this commit, but that's all I could find on the first few pages.
* | Merge pull request #29855 from lugray/has_one_destroyed_by_associationRafael França2017-07-211-0/+2
|\ \ | | | | | | Match destroyed_by_association for has_one to has_many
| * | Match destroyed_by_association for has_one to has_manyLisa Ugray2017-07-211-0/+2
| | | | | | | | | | | | | | | | | | | | | When a has_many association is destroyed by `dependent: destroy`, destroyed_by_association is set to the reflection, and this can be checked in callbacks. This matches that behaviour for has_one associations.
* | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-1935-0/+70
|/ /
* | Merge pull request #29033 from kamipo/make_preload_query_to_prepared_statementsSean Griffin2017-07-181-1/+1
|\ \ | |/ |/| Make preload query to preparable
| * Make preload query to preparableRyuta Kamizono2017-07-071-1/+1
| | | | | | | | | | | | | | Currently preload query cannot be prepared statements even if `prepared_statements: true` due to array handler in predicate builder doesn't support making bind params. This makes preload query to preparable by don't passing array value if possible.
* | Fix unscoping `default_scope` for `Preloader`Ryuta Kamizono2017-07-191-1/+11
| |
* | Fix `JoinDependency` with using a custom tableRyuta Kamizono2017-07-182-6/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, `JoinDependency` doesn't use a custom table alias: ``` % ARCONN=sqlite3 be ruby -w -Itest test/cases/relations_test.rb -n test_using_a_custom_table_with_joins_affects_the_wheres Using sqlite3 Run options: -n test_using_a_custom_table_with_joins_affects_the_wheres --seed 14531 E Error:RelationTest#test_using_a_custom_table_with_joins_affects_the_wheres: ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: posts.author_id: SELECT "omg_posts".* FROM "posts" "omg_posts" INNER JOIN "authors" ON "authors"."id" = "posts"."author_id" WHERE "omg_posts"."title" = ? LIMIT ? ```
* | Don't cache `scope_for_create`Ryuta Kamizono2017-07-162-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I investigated where `scope_for_create` is reused in tests with the following code: ```diff --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -590,6 +590,10 @@ def where_values_hash(relation_table_name = table_name) end def scope_for_create + if defined?(@scope_for_create) && @scope_for_create + puts caller + puts "defined" + end @scope_for_create ||= where_values_hash.merge!(create_with_value.stringify_keys) end ``` It was hit only `test_scope_for_create_is_cached`. This means that `scope_for_create` will not be reused in normal use cases. So we can remove caching `scope_for_create` to respect changing `where_clause` and `create_with_value`.
* | Fix `create_with` using both string and symbolRyuta Kamizono2017-07-163-9/+7
| | | | | | | | | | | | | | This is related with #27680. Since `where_values_hash` keys constructed by `where` are string, so we need `stringify_keys` to `create_with_value` before merging it.
* | Remove useless `aliased_table_name` in `JoinDependency`Ryuta Kamizono2017-07-154-18/+1
|/ | | | If `table.table_alias` is not nil, it is enough to use `table` simply.
* Fix eager loading association with scope including joinsRyuta Kamizono2017-07-041-4/+10
| | | | Fixes #28324.
* Fix preloading association with scope including joinsRyuta Kamizono2017-07-043-49/+25
|
* Remove unused `association_key` and `table` methods in `Preloader::Association`Ryuta Kamizono2017-07-041-10/+0
| | | | These are no longer used since b98668decb9712f26118de57623fd15d7d28646d.
* Merge branch 'master' into require_relative_2017Xavier Noria2017-07-022-4/+12
|\
| * Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-0235-35/+0
| | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
| * Merge pull request #29540 from kirs/rubocop-frozen-stringMatthew Draper2017-07-0235-0/+35
| |\ | | | | | | | | | Enforce frozen string in Rubocop
| | * Enforce frozen string in RubocopKir Shatrov2017-07-0135-0/+35
| | |
| * | Merge pull request #29506 from pat/frozen-string-literalsMatthew Draper2017-07-021-2/+1
| |\ \ | | | | | | | | | | | | Make ActiveSupport frozen-string-literal friendly.
| | * | Make ActiveRecord frozen string literal friendly.Pat Allan2017-06-201-2/+1
| | | |
| * | | Merge pull request #29631 from kamipo/should_be_clear_association_idsMatthew Draper2017-07-011-2/+11
| |\ \ \ | | | | | | | | | | Should be clear `@association_ids` when joined newly associated record
| | * | | Should be clear `@association_ids` when joined newly associated recordRyuta Kamizono2017-06-301-2/+11
| | | |/ | | |/| | | | | | | | | Fixes #29627.
* | / | [Active Record] require => require_relativeAkira Matsuda2017-07-013-3/+3
|/ / /
* / / Delete stale comment for `AR::Associations::Builder::CollectionAssociation`fatkodima2017-07-011-2/+0
|/ /
* | Merge pull request #29616 from kamipo/remove_unused_aliased_table_nameRafael França2017-06-291-8/+0
|\ \ | | | | | | Remove unused `aliased_table_name` in `Association`
| * | Remove unused `aliased_table_name` in `Association`Ryuta Kamizono2017-06-291-8/+0
| | | | | | | | | | | | | | | | | | | | | `aliased_table_name` in `Association` was added at a3502c4. `aliased_table_name` in `JoinDependency` (added at 55854c4) is used, but it looks like that added one in `Association` is never used from the beginning.
* | | Merge pull request #29129 from kamipo/prevent_extra_through_scopeRafael França2017-06-281-2/+4
|\ \ \ | |/ / |/| | Prevent extra `through_scope`
| * | Prevent extra `through_scope`Ryuta Kamizono2017-05-181-2/+4
| | | | | | | | | | | | We can reuse `through_scope` for `reset_association`.
* | | Merge pull request #29610 from ↵Rafael França2017-06-284-7/+7
|\ \ \ | | | | | | | | | | | | | | | | kamipo/dont_passing_klass_connection_to_association_scope Don't passing `klass.connection` to `AssociationScope`
| * | | Don't passing `klass.connection` to `AssociationScope`Ryuta Kamizono2017-06-294-7/+7
| | | | | | | | | | | | | | | | | | | | Passing `klass.connection` is redundant because `AssociationScope` is passed an association itself and an association has `klass`.
* | | | Merge pull request #29604 from ↵Rafael França2017-06-281-4/+1
|\ \ \ \ | |/ / / |/| | | | | | | | | | | kamipo/fix_ids_reader_to_respect_case_sensitive_pk Fix `ids_reader` to respect case sensitive primary key
| * | | Fix `ids_reader` to respect case sensitive primary keyRyuta Kamizono2017-06-281-4/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby car = Car.create!(name: "Tofaş") # Before car.bulb_ids # => SELECT "bulbs".ID FROM "bulbs" WHERE "bulbs"."name" = $1 AND "bulbs"."car_id" = $2 [["name", "defaulty"], ["car_id", 3]] # After car.bulb_ids # => SELECT "bulbs"."ID" FROM "bulbs" WHERE "bulbs"."name" = $1 AND "bulbs"."car_id" = $2 [["name", "defaulty"], ["car_id", 3]] ```
* | | | Merge pull request #29593 from kratob/masterEileen M. Uchitelle2017-06-281-0/+5
|\ \ \ \ | |/ / / |/| | | ActiveRecord: do not create "has many through" records that have been removed
| * | | ActiveRecord: do not create "has many through" records that have been removedTobias Kraze2017-06-281-0/+5
| | | | | | | | | | | | | | | | | | | | If a record was built on a HasManyThroughAssociation, then removed, and then the record was saved, the removed record would be created anyways.