aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #16801 from ↵Rafael Mendonça França2014-09-041-5/+1
|\ | | | | | | | | eileencodes/refactor-scope_chain-on-through-refelction-to-eliminate-branch-in-eval_scope Always add lambda to scope chain to eliminate branch in eval_scope
| * Always add lambda to scope chain to eliminate branch in eval_scopeeileencodes2014-09-041-5/+1
| | | | | | | | | | | | We convert all other scopes to lambda's so it makes sense that we should always returns a lambda on a ThroughReflection as well. This eliminates the need to check if the scope is a Relation.
* | Follup to PR #16762eileencodes2014-09-041-4/+4
|/ | | | | | | | Remove chain from parameters, it's no longer needed since chain and i are being passed via next_reflection Change name of `reflection` to `owner_reflection` because of shadow variable warning. The last reflection will always be the owner.
* get rid of shadowing warning when running tests AR and railtie tests.Yves Senn2014-09-041-2/+2
| | | | | | | | Warning looked like this: ``` /Users/senny/Projects/rails/activerecord/lib/active_record/associations/association_scope.rb:142: warning: shadowing outer local variable - reflection ```
* Break conditional branches into separate methodseileencodes2014-09-011-35/+52
| | | | | | | | | | | | | | | | | | This breaks the two branches of the `if reflection.last` and `else` to clearer see where the two methods can be refactored. Eventually we hope to remove the need for these separated methods altogether. Move the first branch outside the loop This code doesn't need to be in the loop because it it always affects the last chain. `get_bind_values` and `add_constraints` must match in this context because `get_bind_values` is the caching of `add_constraints` Use each_cons to remove need for `chain[i + 1]` The `chain[i + 1]` is confusing because it's not immediately obvious what it's trying to achieve. The use of `each_cons` makes it clear we need to get the `next_reflection`.
* Avoid using heredoc for user warningsGodfrey Chan2014-08-281-6/+6
| | | | | | | | | | Using heredoc would enforce line wrapping to whatever column width we decided to use in the code, making it difficult for the users to read on some consoles. This does make the source code read slightly worse and a bit more error-prone, but this seems like a fair price to pay since the primary purpose for these messages are for the users to read and the code will not stick around for too long.
* [ci skip] Updated documentation syntax of other parameter for rdocTom Kadwill2014-08-271-1/+1
|
* Merge pull request #16705 from tomkadwill/documented_many_block_paramZachary Scott2014-08-261-1/+1
|\ | | | | [ci skip] Updated documentation syntax of block parameter for rdoc
| * [ci skip] Updated documentation syntax of block parameter for rdoc Tom Kadwill2014-08-261-1/+1
| |
* | [ci skip] Updated include to reference record parameter for rdocTom Kadwill2014-08-261-1/+1
|/
* [ci skip] Updated documentation syntax of block parameter for rdocTom Kadwill2014-08-261-1/+1
|
* Only merge scopes with zero arity in has_many throughAgis-2014-08-201-1/+5
| | | | | | | | | | | | | | | with dynamic conditions. Fixes #16128 This bug was introduced in https://github.com/rails/rails/commit/c35e438620f2d56562251571377995359546393d so it's present from 4.1.2-rc1 and after. https://github.com/rails/rails/commit/c35e438620f2d56562251571377995359546393d merges any relation scopes passed as proc objects to the relation, but does *not* take into account the arity of the lambda. To reproduce: https://gist.github.com/Agis-/5f1f0d664d2cd08dfb9b
* Remove to_s from reflection.type in add_constraintseileencodes2014-08-181-2/+2
| | | | | | | The instance var is already saved as a string in the initialization method of AssociationReflection. See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb#L273
* Don't expose these new APIs yet (added in 877ea78 / #16189)Godfrey Chan2014-08-161-1/+1
| | | | | | | WARNING: don't use them! They might change or go away between future beta/RC/ patch releases! Also added a CHANGELOG entry for this.
* Implement `_was` and `changes` for in-place mutations of AR attributesSean Griffin2014-08-161-1/+1
|
* Spelling errorsjbsmith862014-08-141-1/+1
|
* Don't delegate Reflection#chain to ThroughAssociationeileencodes2014-08-041-2/+2
| | | | | | We shouldn't be delegating chain to ThroughAssociation since the only place that needs to call it is `target_scope`. Instead we can call `reflecion.chain`.
* Merge pull request #16378 from ↵Rafael Mendonça França2014-08-021-1/+1
|\ | | | | | | | | JackDanger/doc-fix-in-join-association-build_constraint [doc] updating documented parameter for build_constraint
| * updating documented parameter for build_constraintJack Danger Canty2014-08-021-1/+1
| | | | | | | | | | Updates documentation in line with changes made in 743b67508e2027e1d086142ccbec47a19fc943f6
* | Use available method rather than macro name checkingCarlos Antonio da Silva2014-07-311-1/+1
| |
* | Refactor join_keys to remove complex conditionalseileencodes2014-07-311-8/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Pushing conditionals down to through reflection Only the through association needs the part of this conditional that deals with belongs to and polymorphic? so that can be pushed down into the ThroughReflection reducing the conditionals. Remove conditional because we can delegate join keys to source reflection Remove need for source_macro checking By adding join_id_for to the other reflections we remove the need to cehck against the source_macro and can reduce the conditioanl from the original join_id_for(owner) Using polymorphism instead of testing the source_macro This case statement in join_association is almost exactly the same as the original join_keys code. Testing taht theory by creating a new join_dependency_keys(assoc_klass) method. Refactor join_keys further to be more concise Fixed format of "#:nodoc:" to "# :nodoc:" where I added them to this file.
* | Redefine macro checks for reflectionseileencodes2014-07-301-1/+1
|/ | | | | | | | | | | | | | Now that we define the macro on the reflection type we no longer need to check `macro == :what` on each type for `belongs_to?` or `has_one?` etc. These now default to false unless it's defined in the reflection class. Reuse existing belongs_to? method to check macros We don't need to do `:belongs_to == macro` anymore becasue we have a `belongs_to?` method. I didn't find this being used anywhere for `has_one?` or `collection?` since they were already fixed.
* remove blank lines in the start of the ActiveRecord filesPonomarev Nikolay2014-07-292-2/+0
|
* [ci skip] Fix documentation for @macro and reflection typeseileencodes2014-07-271-1/+1
| | | | | | | | Since `@macro` doesn't exist anymore and these reflections are no longer AssociationReflections but their own types of reflections based on macro I updated the documentation to match the changes I made in #16089 and #16198. An `AssociationReflection` that had a `@macro` of `:has_many` now is a `HasManyReflection`
* Remove unused 1:1 association :remote optionChris Griego2014-07-161-1/+1
| | | This option is unused, left over from pre-1.0 Rails to internally distinguish the location of the foreign key.
* Merge pull request #15266 from dv/use_counter_cache_for_empty_callGodfrey Chan2014-07-151-0/+8
|\ | | | | If a counter_cache exists, use it for #empty?
| * If a counter_cache exists, use it for #empty?David Verhasselt2014-06-101-0/+8
| |
* | Add a `required` option to singular associationsSean Griffin2014-07-042-1/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In addition to defining the association, a `required` association will also have its presence validated. Before: ```ruby belongs_to :account validates_presence_of :account ``` After: ```ruby belongs_to :account, required: true ``` This helps to draw a distinction between types of validations, since validations on associations are generally for data integrity purposes, and aren't usually set through form inputs.
* | Merge pull request #12450 from iantropov/masterRafael Mendonça França2014-06-273-5/+16
|\ \ | | | | | | | | | | | | | | | | | | | | | | | | Fix bug, when ':dependent => :destroy' violates foreign key constraints Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/builder/association.rb activerecord/lib/active_record/associations/builder/has_one.rb
| * | Fix bug, when ':dependent => :destroy' option violates foreign key ↵Ivan Antropov2013-10-263-9/+18
| | | | | | | | | | | | constraints, issue #12380
* | | Deprecate automatic counter caches on has_many :throughSean Griffin2014-06-262-1/+23
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Reliant on https://github.com/rails/rails/pull/15747 but pulled to a separate PR to reduce noise. `has_many :through` associations have the undocumented behavior of automatically detecting counter caches. However, the way in which it does so is inconsistent with counter caches everywhere else, and doesn't actually work consistently. As with normal `has_many` associations, the user should specify the counter cache on the `belongs_to`, if they'd like it updated.
* | | Merge pull request #15847 from sgrif/sg-encapsulate-result-typesRafael Mendonça França2014-06-261-3/+1
|\ \ \ | | | | | | | | Encapsulate knowledge of type objects on `ActiveRecord::Result`
| * | | Encapsulate knowledge of type objects on `ActiveRecord::Result`Sean Griffin2014-06-221-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Attempting to reduce the number of places that care about the details of how type casting occurs. We remove the type casting of the primary key in `JoinDependecy`, rather than encapsulating it. It was originally added for consistency with https://github.com/rails/rails/commit/40898c8c19fa04442fc5f8fb5daf3a8bdb9a1e03#diff-06059df8d3dee3101718fb2c01151ad0R211, but that conditional was later removed in https://github.com/rails/rails/commit/d7ddaa530fd1b94e22d745cbaf2e8a5a34ee9734. What is important is that the same row twice will have the same value for the primary key, which it will.
* | | | `preload` preserves readonly flag on associations. #15853Yves Senn2014-06-251-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | This is a partial fix for #15853. It only works when a `preload` is issued and not an `eager_load`. I've added a skipped failing test-case to keep in mind that we need to deal with `eager_load`.
* | | | Add a deprecation cycle for `NullColumn` from `column_for_attribute`Sean Griffin2014-06-231-2/+2
|/ / / | | | | | | | | | | | | | | | This is public API, and `simple_form` depends on the `nil` return value. We need to go through a deprecation cycle to return a null object. If people want hash access, they can access the hash.
* | | Merge pull request #15747 from sgrif/sg-trolololol-this-is-so-brokenRafael Mendonça França2014-06-192-1/+26
|\ \ \ | | | | | | | | Always update counter caches in memory when adding records
| * | | Always update counter caches in memory when adding recordsSean Griffin2014-06-162-1/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before, calling `size` would only work if it skipped the cache, and would return a different result from the cache, but only if: - The association was previously loaded - Or you called size previously - But only if the size was 0 when you called it This ensures that the counter is appropriately updated in memory.
* | | | Merge pull request #15803 from sgrif/sg-column-in-associationsRafael Mendonça França2014-06-191-2/+2
|\ \ \ \ | | | | | | | | | | Don't rely on the column for type casting reflections
| * | | | Don't rely on the column for type casting reflectionsSean Griffin2014-06-181-2/+2
| |/ / /
* | | | Merge pull request #15772 from nbudin/sti_through_bugRafael Mendonça França2014-06-191-1/+3
|\ \ \ \ | | | | | | | | | | | | | | | Don't include inheritance column in the through_scope_attributes
| * | | | Don't include inheritance column in the through_scope_attributesNat Budin2014-06-171-1/+1
| |/ / /
* / / / Fix has_and_belongs_to_many in a namespaced model pointing to a non ↵Rafael Mendonça França2014-06-191-8/+5
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | namespaced model Now the following case will work fine class Tag < ActiveRecord::Base end class Publisher::Article < ActiveRecord::Base has_and_belongs_to_many :tags end Fixes #15761
* | | Merge pull request #15701 from zzak/issue_15496Rafael Mendonça França2014-06-131-1/+5
|\ \ \ | | | | | | | | Open extension point for defining options in build_through_record
| * | | Open extension point for defining options in build_through_recordZachary Scott2014-06-131-1/+5
| | | | | | | | | | | | | | | | This fixes #15496
* | | | Through associations should set both parent ids on join modelsSean Griffin2014-06-132-7/+15
|/ / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | member = Member.new(club: Club.new) member.save! Before: member.current_membership.club_id # => nil After: member.current_membership.club_id # => club's id
* | / begin refactoring add_constraints by moving join keyseileencodes2014-06-101-12/+3
| |/ |/| | | | | | | | | | | | | | | | | | | | | | | | | add_constraints is complicated and difficult to read. This is the beginning of a long process of refactoring this code. First step: moved the join keys out of AssociationScope and into reflection. We then don't need to call `reflection` because now reflection is `self`. `foreign_key` must be named something else because reflection already has a `foreign_key` method and when passed into `JoinKeys` it was getting the wrong assignment. `reflection_foreign_key` seemed to be an appropriate name. I also named `key` `reflection_key` to match `reflection_foreign_key`.
* | reuse available collection? check instead of macroeileencodes2014-06-092-4/+4
| | | | | | | | | | | | | | Reflection has an available method that is used to check if the reflection is a collection. Any :has_many macro is considered a collection and `collection?` should be used instead of `macro == :has_many`.
* | add has_one? method and reuse instead of checking macroeileencodes2014-06-093-3/+3
| | | | | | | | | | | | Instead of checking for `macro == :has_one` throughout the codebase we can create a `has_one?` method to match the `belongs_to?`, `polymorphic?` and other methods.
* | Rename `type_cast` to `type_cast_from_database`Sean Griffin2014-06-092-2/+2
| | | | | | | | | | | | | | | | In some cases there is a difference between the two, we should always be doing one or the other. For convenience, `type_cast` is still a private method on type, so new types that do not need different behavior don't need to implement two methods, but it has been moved to private so it cannot be used accidentally.
* | Do not try to set the foreign_key again on has_many throughRafael Mendonça França2014-06-091-1/+1
| | | | | | | | | | | | | | Integration tests are inside protected_attributes test suite. Fixes #15496 Fixes rails/protected_attributes#35