aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* Fix for has_many_through counter_cache bugMatthew Robertson2012-12-141-0/+5
| | | | | | This commit fixes reported issue #7630 in which counter caches were not being updated properly when replacing has_many_through relationships
* Replace comments' non-breaking spaces with spacesclaudiob2012-12-041-3/+3
| | | | | | | | | | Sometimes, on Mac OS X, programmers accidentally press Option+Space rather than just Space and don’t see the difference. The problem is that Option+Space writes a non-breaking space (0XA0) rather than a normal space (0x20). This commit removes all the non-breaking spaces inadvertently introduced in the comments of the code.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-12-011-3/+13
|\ | | | | | | | | Conflicts: guides/source/active_record_validations.md
| * copy edits [ci skip]Vijay Dev2012-12-011-1/+1
| |
| * add documentation to CollectionProxy #length and #size methods [ci skip]Francesco Rodriguez2012-11-291-2/+7
| |
| * add documentation to CollectionProxy#empty?Francesco Rodriguez2012-11-291-1/+6
| |
* | Use separate Relation subclasses for each AR classJon Leighton2012-11-302-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At present, ActiveRecord::Delegation compiles delegation methods on a global basis. The compiled methods apply to all subsequent Relation instances. This creates several problems: 1) After Post.all.recent has been called, User.all.respond_to?(:recent) will be true, even if User.all.recent will actually raise an error due to no User.recent method existing. (See #8080.) 2) Depending on the AR class, the delegation should do different things. For example, if a Post.zip method exists, then Post.all.zip should call it. But this will then result in User.zip being called by a subsequent User.all.zip, even if User.zip does not exist, when in fact User.all.zip should call User.all.to_a.zip. (There are various variants of this problem.) We are creating these compiled delegations in order to avoid method missing and to avoid repeating logic on each invocation. One way of handling these issues is to add additional checks in various places to ensure we're doing the "right thing". However, this makes the compiled methods signficantly slower. In which case, there's almost no point in avoiding method_missing at all. (See #8127 for a proposed solution which takes this approach.) This is an alternative approach which involves creating a subclass of ActiveRecord::Relation for each AR class represented. So, with this patch, Post.all.class != User.all.class. This means that the delegations are compiled for and only apply to a single AR class. A compiled method for Post.all will not be invoked from User.all. This solves the above issues without incurring significant performance penalties. It's designed to be relatively seamless, however the downside is a bit of complexity and potentially confusion for a user who thinks that Post.all and User.all should be instances of the same class. Benchmark --------- require 'active_record' require 'benchmark/ips' class Post < ActiveRecord::Base establish_connection adapter: 'sqlite3', database: ':memory:' connection.create_table :posts def self.omg :omg end end relation = Post.all Benchmark.ips do |r| r.report('delegation') { relation.omg } r.report('constructing') { Post.all } end Before ------ Calculating ------------------------------------- delegation 4392 i/100ms constructing 4780 i/100ms ------------------------------------------------- delegation 144235.9 (±27.7%) i/s - 663192 in 5.038075s constructing 182015.5 (±21.2%) i/s - 850840 in 5.005364s After ----- Calculating ------------------------------------- delegation 6677 i/100ms constructing 6260 i/100ms ------------------------------------------------- delegation 166828.2 (±34.2%) i/s - 754501 in 5.001430s constructing 116575.5 (±18.6%) i/s - 563400 in 5.036690s Comments -------- Bear in mind that the standard deviations in the above are huge, so we can't compare the numbers too directly. However, we can conclude that Relation construction has become a little slower (as we'd expect), but not by a huge huge amount, and we can still construct a large number of Relations quite quickly.
* | Ensure that associations have a symbol argument.Steve Klabnik2012-11-281-0/+2
|/ | | | Fixes #7418.
* Merge pull request #8291 from senny/8265_build_with_polymorphic_associationRafael Mendonça França2012-11-221-1/+2
|\ | | | | | | | | | | | | prevent mass assignment of polymorphic type when using `build` Conflicts: activerecord/CHANGELOG.md
| * prevent mass assignment of polymorphic type when using `build`Yves Senn2012-11-221-1/+2
| | | | | | | | Closes #8265
* | Remove the #sum method from CollectionAssociationCarlos Antonio da Silva2012-11-211-9/+0
|/ | | | | | | Since edd94cee9af1688dd036fc58fd405adb30a5e0da, CollectionProxy delegates all calculation methods - except count - to the scope, which does basically what this method was doing, but since we're delegating from the proxy, the association method was never called.
* Do not create useless database transaction when building `has_one` association.Bogdan Gusiev2012-11-101-1/+9
|
* fix warningsJon Leighton2012-11-091-1/+1
|
* Merge pull request #8116 from senny/7993_configure_counter_cache_for_has_manyJon Leighton2012-11-092-2/+2
|\ | | | | :counter_cache option for to support custom named counter caches
| * :counter_cache option for to support custom named counter caches. Closes #7993Yves Senn2012-11-042-2/+2
| |
* | Delegate all calculations to the scope.Jon Leighton2012-11-091-4/+2
| | | | | | | | | | | | | | So that the scope may be a NullRelation and return a result without executing a query. Fixes #7928
* | CollectionProxy#pluck issues no query for a new_record? ownerJon Leighton2012-11-091-0/+4
| | | | | | | | | | | | | | Fixes #8102. I couldn't find a nicer way to deal with this than delegate the call to #scope, which will be a NullRelation when we want it to be.
* | Nullify the relation at a more general level.Jon Leighton2012-11-092-6/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This allows us to avoid hacks like the "return 0 if owner.new_record?" in #count (which this commit removes). Also, the relevant foreign key may actually be present even on a new owner record, in which case we *don't* want a null relation. This logic is encapsulated in the #null_scope? method. We also need to make sure that the CollectionProxy is not 'infected' with the NullRelation module, or else the methods from there will override the definitions in CollectionProxy, leading to incorrect results. Hence the nullify: false option to CollectionAssociation#scope. (This feels a bit nasty but I can't think of a better way.)
* | Relations built off collection associations with an unsaved owner should be ↵Jon Leighton2012-11-091-2/+3
| | | | | | | | | | | | | | | | | | | | null relations For example, the following should not run any query on the database: Post.new.comments.where(body: 'omg').to_a # => [] Fixes #5215.
* | adding requires for constant dependenciesAaron Patterson2012-11-071-0/+3
|/
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-11-034-6/+6
|\ | | | | | | | | | | | | Conflicts: actionpack/lib/action_controller/metal/mime_responds.rb activerecord/lib/active_record/attribute_methods.rb guides/source/working_with_javascript_in_rails.md
| * This is comment for singular association.kennyj2012-11-021-1/+1
| |
| * fix a typo in comments to ActiveRecord::Associations::Association.stale_stateAndrii Dovgaliuk2012-10-241-1/+1
| |
| * Migration of docs to 1.9 hash syntaxAvnerCohen2012-10-232-4/+4
| |
* | Fix issue with collection associations and first(n)/last(n)Carlos Antonio da Silva2012-11-011-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When calling first(n) or last(n) in a collection, Active Record was improperly trying to set the inverse of instance in case that option existed. This change was introduced by fdf4eae506fa9895e831f569bed3c4aa6a999a22. In such cases we don't need to do that "manually", since the way collection will be loaded will already handle that, so we just skip setting the inverse association when any argument is given to first(n)/last(n). The test included ensures that these scenarios will have the inverse of instance set properly. Fixes #8087, Closes #8094. Squashed cherry-pick from d37d40b and c368b66. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/associations/collection_association.rb
* | Make caller attribute in deprecation methods optionalAlexey Gaziev2012-10-301-1/+1
| |
* | Provide a call stack for deprecation warnings where needed.Nikita Afanasenko2012-10-291-1/+1
| | | | | | | | It's sometimes hard to quickly find where deprecated call was performed, especially in case of migrating between Rails versions. So this is an attempt to improve the call stack part of the warning message by providing caller explicitly.
* | Remove ActiveRecord::ModelJon Leighton2012-10-261-1/+1
|/ | | | | | | | | | In the end I think the pain of implementing this seamlessly was not worth the gain provided. The intention was that it would allow plain ruby objects that might not live in your main application to be subclassed and have persistence mixed in. But I've decided that the benefit of doing that is not worth the amount of complexity that the implementation introduced.
* Eager autoload Preloader classesJohn Firebaugh2012-10-101-10/+14
| | | | | Without eager autoloading, these would be autoloaded only when #preloader_for is called, which is too late in threaded applications.
* Fix has_many assocation w/select load after createErnie Miller2012-10-051-1/+1
| | | | | | | | | | | | | | If you create a new record via a collection association proxy that has not loaded its target, and which selects additional attributes through the association, then when the proxy loads its target, it will inadvertently trigger an ActiveModel::MissingAttributeError during attribute writing when CollectionAssociation#merge_target_lists attempts to do its thing, since the newly loaded records will possess attributes the created record does not. This error also raises a bogus/confusing deprecation warning when accessing the association in Rails 3.2.x, so cherry-pick would be appreciated!
* Revert "Use flat_map { } instead of map {}.flatten"Santiago Pastorino2012-10-053-4/+4
| | | | | | | | | | | This reverts commit abf8de85519141496a6773310964ec03f6106f3f. We should take a deeper look to those cases flat_map doesn't do deep flattening. irb(main):002:0> [[[1,3], [1,2]]].map{|i| i}.flatten => [1, 3, 1, 2] irb(main):003:0> [[[1,3], [1,2]]].flat_map{|i| i} => [[1, 3], [1, 2]]
* Use flat_map { } instead of map {}.flattenSantiago Pastorino2012-10-053-4/+4
|
* Count returns 0 without querying if parent is not savedFrancesco Rodriguez2012-10-031-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | Patches `CollectionAssociation#count` to return 0 without querying if the parent record is new. Consider the following code: class Account has_many :dossiers end class Dossier belongs_to :account end a = Account.new a.dossiers.build # before patch a.dossiers.count # SELECT COUNT(*) FROM "dossiers" WHERE "dossiers"."account_id" IS NULL # => 0 # after a.dosiers.count # fires without sql query # => 0 Fixes #1856.
* Merge branch 'master' of github.com:lifo/docrailsVijay Dev2012-09-281-24/+8
|\ | | | | | | | | Conflicts: actionpack/lib/action_view/helpers/asset_tag_helper.rb
| * fix AR::Associations::CollectionProxy#delete broken documentation [ci skip]Francesco Rodriguez2012-09-211-23/+2
| |
| * update AR::Associations::CollectionProxy#loaded? documentation [ci skip]Francesco Rodriguez2012-09-211-1/+6
| |
* | Fix destructive side effects from marshaling an association caused by ↵Jeremy Kemper2012-09-251-5/+2
|/ | | | 65843e1acc0c8d285ff79f8c9c49d4d1215440be
* Merge pull request #7251 from rails/integrate-strong_parametersDavid Heinemeier Hansson2012-09-185-30/+29
|\ | | | | Integrate strong_parameters in Rails 4
| * Remove mass_assignment_options from ActiveRecordGuillermo Iguaran2012-09-165-29/+29
| |
| * Remove mass assignment security from ActiveRecordGuillermo Iguaran2012-09-161-1/+0
| |
* | Merge pull request #7661 from ernie/build-join-records-on-unsaved-hmtRafael Mendonça França2012-09-171-0/+14
|\ \ | |/ |/| Fix collection= on hm:t join models when unsaved
| * Fix collection= on hm:t join models when unsavedErnie Miller2012-09-171-0/+14
| | | | | | | | | | | | If assigning to a has_many :through collection against an unsaved object using the collection=[<array_of_items>] syntax, the join models were not properly created, previously.
* | Remove debug code :bomb:Rafael Mendonça França2012-09-161-1/+0
| |
* | Don't preserve SELECT columns on COUNTSteve Klabnik2012-09-161-1/+2
|/ | | | | | | | | | | | | | | | | | The COUNT clause of a finder_sql relationship is being rewritten from COUNT(*) to COUNT(table_name.*). This does not appear to be valid syntax in MySQL: ``` mysql> SELECT COUNT( table_name.* ) FROM `table_name`; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* ) FROM `table_name`' at line 1 ``` This fixes the bug, as well as adding tests so we don't re-introduce it in the future. Fixes #3956.
* Update documentation for CollectionProxyMarc-Andre Lafortune2012-09-121-8/+2
|
* Merge pull request #4976 from kreynolds/fix_eager_without_pkeyJon Leighton2012-09-071-1/+1
|\ | | | | Fix eagerly loading associations without primary keys
| * Fix eagerly loading associations without primary keysKelley Reynolds2012-02-091-1/+1
| |
* | Fix grammarJo Liss2012-08-301-3/+3
| |
* | Use inversed parent for first and last child of has_many associationbrainopia2012-08-181-1/+1
| |
* | Use method compilation for association methodsJon Leighton2012-08-105-64/+62
| | | | | | | | | | | | | | | | | | Method compilation provides better performance and I think the code comes out cleaner as well. A knock on effect is that methods that get redefined produce warnings. I think this is a good thing. I had to deal with a bunch of warnings coming from our tests, though.