aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
...
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-05-195-7/+34
|\
| * copy edits[ci skip]Vijay Dev2013-05-191-11/+8
| |
| * Added some more documentation for define_readers and define_writer of the ↵aditya-kapoor2013-05-153-0/+15
| | | | | | | | Association and its inherited classes
| * Added some more documentation for ↵aditya-kapoor2013-05-151-0/+11
| | | | | | | | ActiveRecord::Associations::Builder::Association class
| * Added documentation for ActiveRecord::Associations::Builder::Association classaditya-kapoor2013-05-151-0/+4
| |
| * emphasize that callbacks are called in destroy_allNeeraj Singh2013-05-132-7/+7
| | | | | | | | | | | | | | Cleaned up rdoc a bit emphasizing that callbacks are called. Also removed the stress on the fact that records are always removed. If callbacks return false then records will not be deleted.
* | stop doing assingments in an iteratorAaron Patterson2013-05-171-2/+6
|/
* Extract JoinDependency#join_relation to DRY the repeated application of the ↵Ben Woosley2013-05-101-0/+7
| | | | #join_associations.
* Move #proxy_association method to AssociationRelationJon Leighton2013-05-101-3/+1
|
* Set the inverse when association queries are refinedJon Leighton2013-05-101-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | Suppose Man has_many interests, and inverse_of is used. Man.first.interests.first.man will correctly execute two queries, avoiding the need for a third query when Interest#man is called. This is because CollectionAssociation#first calls set_inverse_instance. However Man.first.interests.where("1=1").first.man will execute three queries, even though this is obviously a subset of the records in the association. This is because calling where("1=1") spawns a new Relation object from the CollectionProxy object, and the Relation has no knowledge of the association, so it cannot set the inverse instance. This commit solves the problem by making relations spawned from CollectionProxies return a new Relation subclass called AssociationRelation, which does know about associations. Records loaded from this class will get the inverse instance set properly. Fixes #5717. Live commit from La Conf! :sparkles:
* Created a method to automatically find inverse associations and cachewangjohn2013-05-072-2/+2
| | | | | | the results. Added tests to check to make sure that inverse associations are automatically found when has_many, has_one, or belongs_to associations are defined.
* Merge pull request #10489 from greenriver/ar_counter_cache_multiple_destroyRafael Mendonça França2013-05-061-1/+3
|\ | | | | | | | | | | | | Confirm a record has not already been destroyed before decrementing counter cache Conflicts: activerecord/CHANGELOG.md
| * Confirm a record has not already been destroyed before decrementingBen Tucker2013-05-061-1/+3
| | | | | | | | | | | | | | | | | | counter cache At present, calling destroy multiple times on the same record results in the belongs_to counter cache being decremented multiple times. With this change the record is checked for whether it is already destroyed prior to decrementing the counter cache.
* | Do not overwrite manually built records during one-to-one nested attribute ↵Olek Janiszewski2013-05-031-4/+8
|/ | | | | | | | | | | | | | | | | | | | | assignment For one-to-one nested associations, if you build the new (in-memory) child object yourself before assignment, then the NestedAttributes module will not overwrite it, e.g.: class Member < ActiveRecord::Base has_one :avatar accepts_nested_attributes_for :avatar def avatar super || build_avatar(width: 200) end end member = Member.new member.avatar_attributes = {icon: 'sad'} member.avatar.width # => 200
* Fix #8856 Ensure has_one association=(associate) triggers save.Chris Thompson2013-04-301-3/+2
| | | | | | | | | | | | | | | | | | | | | | | activerecord/lib/active_record/associations.rb states: # [association=(associate)] # Assigns the associate object, extracts the primary key, sets it as the foreign key, # and saves the associate object. Since commit 42dd5d9f2976677a4bf22347f2dde1a8135dfbb4 to fix #7191, this is no longer the case if the associate has changed, but is the same object. For example: # Pirate has_one :ship pirate = Pirate.create!(catchphrase: "A Pirate") ship = pirate.build_ship(name: 'old name') ship.save! ship.name = 'new name' pirate.ship = ship That last line should trigger a save. Although we are not changing the association, the associate (ship) has changed.
* Lookup the class at runtime, not when the association is builtAndrew White2013-04-241-2/+2
| | | | | | | | | Trying to lookup the parent class when the association is being built runs the risk of generating uninitialized constant errors because classes haven't been fully defined yet. To work around this we look up the class at runtime through the `association` method. Fixes #10197.
* Revert "Revert "`belongs_to :touch` behavior now touches old association ↵Andrew White2013-04-241-1/+12
| | | | | | | | | when transitioning to new association" until a proper fix is found for #10197" This reverts commit 7389df139a35436f00876c96d20e81ba23c93f0a. Conflicts: activerecord/test/cases/timestamp_test.rb
* Revert "`belongs_to :touch` behavior now touches old association when ↵David Heinemeier Hansson2013-04-231-12/+1
| | | | transitioning to new association" until a proper fix is found for #10197
* Update counter cache when pushing into associationMatthew Robertson2013-04-211-3/+4
| | | | | | | | | | | | | | | | This commit fixes a regression bug in which counter_cache columns were not being updated correctly when newly created records were being pushed into an assocation. EG: # this was fine @post.comment.create! # this was fine @comment = Comment.first @post.comments << @comment # this would not update counters @post.comments << Comment.create!
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-04-113-4/+4
|\ | | | | | | | | Conflicts: guides/source/action_mailer_basics.md
| * JoinPart is no longer an abstract classNeeraj Singh2013-04-081-1/+1
| |
| * minor copy editingNeeraj Singh2013-03-302-3/+3
| |
* | rdoc for some of the methods in JoinDependencyNeeraj Singh2013-04-102-0/+36
| |
* | remove_duplicate_results! should be protectedNeeraj Singh2013-04-101-2/+2
| |
* | While merging relations preserve context for joinsJared Armstrong and Neeraj Singh2013-04-101-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #3002. Also see #5494. ``` class Comment < ActiveRecord::Base belongs_to :post end class Author < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :author has_many :comments end ``` `Comment.joins(:post).merge(Post.joins(:author).merge(Author.where(:name => "Joe Blogs"))).all` would fail with `ActiveRecord::ConfigurationError: Association named 'author' was not found on Comment`. It is failing because `all` is being called on relation which looks like this after all the merging: `{:joins=>[:post, :author], :where=>[#<Arel::Nodes::Equality: ....}`. In this relation all the context that `Post` was joined with `Author` is lost and hence the error that `author` was not found on `Comment`. Ths solution is to build JoinAssociation when two relations with join information are being merged. And later while building the arel use the previously built `JoinAssociation` record in `JoinDependency#graft` to build the right from clause. Thanks to Jared Armstrong (https://github.com/armstrjare) for most of the work. I ported it to make it compatible with new code base.
* | changed variable name active_record => base_klassNeeraj Singh2013-04-094-12/+12
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Current code stores the klass name in active_record and this is used throughout. While reviewing the code time and again I had the mental picture of active_record being an instance of a klass. However here the actual klass is being stored in @active_record. Secondly at two different places while referring to @active_record the comment refers to it as base klass. All this points to active_record being not the best variable name. So I thought it is better to replace active_record with base_klass. This change is confined to JoinDependency, JoinBase, JoinPart and JoinAssociation - all joining related work.
* | Avoid iterating over records hash when not necessaryCarlos Antonio da Silva2013-04-071-2/+6
| | | | | | | | | | | | If the reflection scope is not flagged with distinct value, there is no need to iterate over the records, so we avoid that by doing the check before iterating rather than inside the iteration block.
* | each to each_value; remove unused varsVipul A M2013-04-071-1/+1
| |
* | Merge pull request #9996 from mikz/masterJon Leighton2013-04-051-0/+1
|\ \ | | | | | | Association with inverse_of does not set the parent in association building block
| * | fix inverse_of association in block of new childMichal Cichra2013-04-011-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes inconsistency when building children of association which has inverse_of set properly. When creating new association object with a block: parent.association.build do |child| child.parent.equal?(parent) # false end So the block the `child.parent` did not point to the same object. But when the object is created it points to same instance: child = parent.association.build child.parent.equal?(parent) # true
* | | Merge pull request #10058 from jamesgolick/masterJon Leighton2013-04-051-4/+2
|\ \ \ | | | | | | | | Avoid calling define_method in CollectionProxy#scope
| * | | Refactor CollectionProxy#scope to avoid calling #extend.James Golick2013-04-021-4/+2
| | | |
* | | | Avoid an attempt to fetch old record when id was not present in touch callbackCarlos Antonio da Silva2013-04-041-7/+7
| | | |
* | | | Use the correct pk field from the reflected class to find the old recordCarlos Antonio da Silva2013-04-041-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The implementation was using the source class foreign key field instead of the reflected primary key one to find the old record. For instance, for this scenario class Bulb < ActiveRecord::Base belongs_to :car, :touch => true end class Car < ActiveRecord::Base has_many :bulbs end the current implementation was trying to do this query: Car.where(car_id: X).first where we should be doing this query: Car.where(id: X).first This should hopefully fix the build.
* | | | Use inspect when writing the foreign key from the reflectionAndrew White2013-04-051-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | If we don't use inspect inside the class_eval block then the foreign key is written without quotes causing us to fetch the foreign key value and not the column name.
* | | | use | to have more intent revealing codeNeeraj Singh2013-04-041-1/+1
| | | | | | | | | | | | | | | | thanks to @egilburg for suggestion
* | | | Merge pull request #10087 from neerajdotname/10016Carlos Antonio da Silva2013-04-041-0/+1
|\ \ \ \ | | | | | | | | | | has_many through obeys order on through association
| * | | | has_many through obeys order on through associationNeeraj Singh2013-04-041-0/+1
| | | | | | | | | | | | | | | | | | | | fixes #10016
* | | | | Merge pull request #10088 from neerajdotname/fixes1Rafael Mendonça França2013-04-042-5/+5
|\ \ \ \ \ | |/ / / / |/| | | | minor fixes including variable name change and expanded rdoc
| * | | | show name of the klass that has missing associationNeeraj Singh2013-04-021-1/+1
| | | | |
| * | | | not a relation. it's an arel select managerNeeraj Singh2013-04-021-4/+4
| |/ / /
* | | | Merge pull request #9141 from adamgamble/issue-9091David Heinemeier Hansson2013-04-031-0/+10
|\ \ \ \ | | | | | | | | | | belongs_to :touch should touch old record when transitioning.
| * | | | Modifies belong_to touch callback to touch old associations also #9091Adam Gamble2013-03-141-0/+10
| | | | |
* | | | | Merge pull request #10049 from vipulnsward/optimize_around_merge_v1Rafael Mendonça França2013-04-031-1/+1
|\ \ \ \ \ | | | | | | | | | | | | optimize some code around merge
| * | | | | optimize some code around mergeVipul A M2013-04-031-1/+1
| | |_|_|/ | |/| | |
* | | | | Move alias method near to the aliased one in collection proxyCarlos Antonio da Silva2013-04-031-2/+1
| |_|/ / |/| | |
* | | | no need to invoke to_s before applying internNeeraj Singh2013-04-021-1/+1
| | | |
* | | | Updated the error message for +find+ on an inverse_of associationwangjohn2013-04-011-1/+1
| | | | | | | | | | | | | | | | | | | | so that it is consistent with the error thrown for +find+ without an inverse_of association.
* | | | Throwing a RecordNotFound exception when a record is scanned using thewangjohn2013-04-011-1/+13
|/ / / | | | | | | | | | | | | inverse_of option. I've also refactored the code for raising a RecordNotFound exception when searching for records with ids.
* | | Merge branch 'master' of github.com:lifo/docrailsVijay Dev2013-03-301-1/+2
|\ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Conflicts: activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb activerecord/test/cases/adapter_test.rb guides/source/testing.md [ci skip]