aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/through_association.rb
Commit message (Collapse)AuthorAgeFilesLines
* modernizes hash syntax in activerecordXavier Noria2016-08-061-1/+1
|
* Fix misleading errors for has_one through relationsMehmet Emin İNAÇ2015-07-221-2/+10
|
* Correct through associations using scopesSean Griffin2015-06-301-6/+0
| | | | | | | | | | | | | | | | | | | | | | 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.
* Doc fix [ci skip]Sushruth Sivaramakrishnan2015-03-071-1/+1
|
* Don't rely on relation mutability when building through associationsSean Griffin2015-01-241-1/+1
| | | | | | | | | | | | | | | | | | | | | | Specifically, the issue is relying on `where_unscoping` mutating the where values. It does not, however, mutate the bind values, which could cause an error under certain circumstances. This was not exposed by the tests, since the only place which would have been affected is unscoping a boolean, which doesn't go through prepared statements. I had a hard time getting better test coverage to demonstrate the issue. This in turn, caused `merge` to go through proper logic, and try to clear out the binds associated with the unscoped relation, which then exposed a source of `nil` for the columns, as binds weren't expanding `{ "posts.id" => 1 }` to `{ "posts" => { "id" => 1 } }`. This has been fixed. The bulk of `create_binds` needed to be moved to a separate method, since the dot notation should not be expanded recursively. I'm pretty sure this removes a subtle quirk that a ton of code in `Relation::Merger` is working around, and I suspect that code can be greatly simplified. However, unraveling that rats nest is no small task.
* Merge pull request #15309 from iantropov/issue_12698_build_throughRafael Mendonça França2015-01-021-0/+11
|\ | | | | | | | | | | | | | | Add setting of FK for throgh associations while building Conflicts: activerecord/CHANGELOG.md activerecord/test/cases/associations/has_many_through_associations_test.rb
| * Add setting of FK for throgh associations while buildingIvan Antropov2014-05-251-0/+11
| |
* | 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
* | 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`.
* | Use available method rather than macro name checkingCarlos Antonio da Silva2014-07-311-1/+1
| |
* | Through associations should set both parent ids on join modelsSean Griffin2014-06-131-6/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | member = Member.new(club: Club.new) member.save! Before: member.current_membership.club_id # => nil After: member.current_membership.club_id # => club's id
* | reuse available belongs_to? methodeileencodes2014-06-031-3/+2
|/ | | | | | | | | | Reflection has a `belongs_to?` method. Instead of checking for `macro == :belongs_to` throughout the source reuse existing method. I also bumped `foreign_key_present?` method onto on line because the `belongs_to?` makes it shorter than other longer lines in the same class.
* Merge pull request #14573 from habermann24/has_many_through_fixRafael Mendonça França2014-04-221-2/+4
|\ | | | | | | | | | | | | | | Properly handle scoping with has_many :through. Fixes #14537. Conflicts: activerecord/CHANGELOG.md activerecord/test/cases/associations/has_many_through_associations_test.rb
| * Minor improvement: Use the merge method on the relation instead of ↵Jan Habermann2014-04-031-1/+1
| | | | | | | | instance_eval directly.
| * Simplify the code in target_scopeJan Habermann2014-04-031-5/+2
| |
| * Properly handle scoping with has_many :through. Fixes #14537.Jan Habermann2014-04-031-2/+7
|/
* use drop and avoid a range objectAaron Patterson2013-08-011-1/+1
|
* Simplify/fix implementation of default scopesJon Leighton2013-06-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The previous implementation was necessary in order to support stuff like: class Post < ActiveRecord::Base default_scope where(published: true) scope :ordered, order("created_at") end If we didn't evaluate the default scope at the last possible moment before sending the SQL to the database, it would become impossible to do: Post.unscoped.ordered This is because the default scope would already be bound up in the "ordered" scope, and therefore wouldn't be removed by the "Post.unscoped" part. In 4.0, we have deprecated all "eager" forms of scopes. So now you must write: class Post < ActiveRecord::Base default_scope { where(published: true) } scope :ordered, -> { order("created_at") } end This prevents the default scope getting bound up inside the "ordered" scope, which means we can now have a simpler/better/more natural implementation of default scoping. A knock on effect is that some things that didn't work properly now do. For example it was previously impossible to use #except to remove a part of the default scope, since the default scope was evaluated after the call to #except.
* optimize some code around mergeVipul A M2013-04-031-1/+1
|
* Migration of docs to 1.9 hash syntaxAvnerCohen2012-10-231-1/+1
|
* Deprecate ActiveRecord::Base.scoped.Jon Leighton2012-07-271-1/+1
| | | | | | | It doesn't serve much purpose now that ActiveRecord::Base.all returns a Relation. The code is moved to active_record_deprecated_finders.
* @stale_state should be nil when a model isn't saved.kennyj2012-04-131-1/+1
|
* Not need to pass join attributes to association buildRafael Mendonça França2012-03-071-3/+7
|
* Fix #3247.Jon Leighton2011-11-031-1/+1
| | | | | Fixes creating records in a through association with a polymorphic source type.
* Don't include any of includes, preload, joins, eager_load in the through ↵Jon Leighton2011-09-071-1/+1
| | | | association scope.
* Nested through associations: preloads from the default scope of a through ↵Jon Leighton2011-09-061-1/+1
| | | | model should not be included in the association scope. (We're already excluding includes.) Fixes #2834.
* Ignore :includes on through associationsAndrew White2011-05-241-1/+1
|
* Don't use select() values from the join model of a through association. ↵Jon Leighton2011-05-111-3/+4
| | | | Fixes #508.
* Don't use mass-assignment protection when applying the ↵Jon Leighton2011-05-101-1/+3
| | | | scoped.scope_for_create. Fixes #481.
* Resolve some TODO comments which I decided did not need anything doneJon Leighton2011-03-121-7/+4
|
* Rename Reflection#through_reflection_chain and #through_options to ↵Jon Leighton2011-03-101-3/+2
| | | | Reflection#chain and Reflection#options as they now no longer relate solely to through associations.
* Move the code which builds a scope for through associations into a generic ↵Jon Leighton2011-03-101-128/+0
| | | | AssociationScope class which is capable of building a scope for any association.
* Fix ↵Jon Leighton2011-03-071-1/+1
| | | | test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys
* Refactor ThroughAssociation#join_to to be much smaller, and independent of ↵Jon Leighton2011-03-061-84/+28
| | | | construct_owner_conditions.
* Refactor ThroughAssociation#tables to just be a flat array of tables in the ↵Jon Leighton2011-03-061-75/+70
| | | | order that they should be joined together.
* Push source_type and polymorphic conditions out of ThroughAssociation and ↵Jon Leighton2011-03-051-17/+0
| | | | JoinDependency::JoinAssociation and into the reflection instead.
* Add a test for STI on the through where the through is nested, and change ↵Jon Leighton2011-03-051-13/+22
| | | | the code which support this
* Merge branch 'master' into nested_has_many_throughJon Leighton2011-03-041-49/+172
| | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: activerecord/CHANGELOG activerecord/lib/active_record/association_preload.rb activerecord/lib/active_record/associations.rb activerecord/lib/active_record/associations/class_methods/join_dependency.rb activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb activerecord/lib/active_record/associations/has_many_association.rb activerecord/lib/active_record/associations/has_many_through_association.rb activerecord/lib/active_record/associations/has_one_association.rb activerecord/lib/active_record/associations/has_one_through_association.rb activerecord/lib/active_record/associations/through_association_scope.rb activerecord/lib/active_record/reflection.rb activerecord/test/cases/associations/has_many_through_associations_test.rb activerecord/test/cases/associations/has_one_through_associations_test.rb activerecord/test/cases/reflection_test.rb activerecord/test/cases/relations_test.rb activerecord/test/fixtures/memberships.yml activerecord/test/models/categorization.rb activerecord/test/models/category.rb activerecord/test/models/member.rb activerecord/test/models/reference.rb activerecord/test/models/tagging.rb
* Delegate through_reflection and source_reflection to reflectionJon Leighton2011-02-211-23/+23
|
* Delegate Association#options to the reflection, and replace ↵Jon Leighton2011-02-211-9/+11
| | | | 'reflection.options' with 'options'. Also add through_options and source_options methods for through associations.
* Associations - where possible, call attributes methods rather than directly ↵Jon Leighton2011-02-211-34/+34
| | | | accessing the instance variables
* Add :nodoc: to ThroughAssociation and HasOneAssociationJon Leighton2011-02-181-1/+1
|
* Add interpolation of association conditions back in, in the form of proc { ↵Jon Leighton2011-02-141-2/+2
| | | | ... } rather than instance_eval-ing strings
* Remove Relation#& alias for Relation#mergeErnie Miller2011-02-121-1/+1
|
* Support the :dependent option on has_many :through associations. For ↵Jon Leighton2011-02-071-7/+26
| | | | historical and practical reasons, :delete_all is the default deletion strategy employed by association.delete(*records), despite the fact that the default strategy is :nullify for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.
* Make use of helpers in AssociationReflectionJon Leighton2011-01-301-4/+4
|
* Has many through - It is not necessary to manually merge in the conditions ↵Jon Leighton2011-01-301-4/+0
| | | | hash for the through record, because the creation is done directly on the through association, which will already handle setting the conditions.
* Don't pass around conditions as strings in ThroughAssociationJon Leighton2011-01-301-30/+25
|
* Indent methods under private/protected sectionsJon Leighton2011-01-301-101/+101
|
* Let's be less blasé about method visibility on association proxiesJon Leighton2011-01-301-7/+9
|