aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations
Commit message (Collapse)AuthorAgeFilesLines
* 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
| |
* | Remove support to activerecord-deprecated_findersRafael Mendonça França2015-01-029-76/+48
| |
* | Share foreign_key_present? implementation in _has_ associationsbrainopia2014-12-313-8/+13
| |
* | Ensure `first!` and friends work on loaded associationsSean Griffin2014-12-291-0/+1
| | | | | | | | Fixes #18237
* | Pass a type caster when aliasing tables for joinsSean Griffin2014-12-293-6/+11
| |
* | `eager_load` preserves readonly flag for associationsTakashi Kokubun2014-12-301-0/+1
| |
* | Go through normal `where` logic when preloading associationsSean Griffin2014-12-261-1/+1
| | | | | | | | | | | | | | | | | | | | This will allow eager type casting to take place as needed. There doesn't seem to be any particular reason that the `in` statement was forced for single values, and the commit message where it was introduced gives no context. See https://github.com/rails/rails/commit/d90b4e2615e8048fdeffc6dffe3246704adee01f
* | Remove `klass` and `arel_table` as a dependency of `PredicateBuilder`Sean Griffin2014-12-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This class cares far too much about the internals of other parts of Active Record. This is an attempt to break out a meaningful object which represents the needs of the predicate builder. I'm not fully satisfied with the name, but the general concept is an object which represents a table, the associations to/from that table, and the types associated with it. Many of these exist at the `ActiveRecord::Base` class level, not as properties of the table itself, hence the need for another object. Currently it provides these by holding a reference to the class, but that will likely change in the future. This allows the predicate builder to remain wholy concerned with building predicates. /cc @mrgilman
* | Inject the `PredicateBuilder` into the `Relation` instanceSean Griffin2014-12-263-4/+11
| | | | | | | | | | | | | | Construction of relations can be a hotspot, we don't want to create one of these in the constructor. This also allows us to do more expensive things in the predicate builder's constructor, since it's created once per AR::Base subclass
* | - Changed `target` to `target_reflection` to avoid warning `possible ↵Vipul A M2014-12-251-2/+2
| | | | | | | | reference to past scope`
* | Remove unneeded special case to calculate size for has_many :throughBogdan Gusiev2014-12-231-15/+0
| | | | | | | | | | All cases are properly handled in CollectionAssociation for all subclasses of this association
* | Improve the performance of reading belongs_to associationsSean Griffin2014-12-221-5/+6
| | | | | | | | | | | | | | | | | | | | | | | | `ActiveRecord::Base#[]` has overhead that was introduced in 4.2. The `foo["id"]` working with PKs other than ID isn't really a case that we want to support publicly, but deprecating was painful enough that we avoid it. `_read_attribute` was introduced as the faster alternative for use internally. By using that, we can save a lot of overhead. We also save some overhead by reading the attribute one fewer times in `stale_state`. Fixes #18151
* | Pass connection rather than alias_trackereileencodes2014-12-131-16/+17
| | | | | | | | | | | | | | | | | | Because we're only using the `connection` so passing the entire tracker isn't unnecessary. Eventually only the `connection` will be passed to `add_constraints` with later refactoring but curretly that's not possible because of `construct_tables` method.
* | Add foreign_type option for polymorphic has_one and has_many.Ulisses Almeida + Kassio Borges2014-12-082-2/+2
| | | | | | | | | | | | | | To be possible to use a custom column name to save/read the polymorphic associated type in a has_many or has_one polymorphic association, now users can use the option :foreign_type to inform in what column the associated object type will be saved.
* | Update Arel usage for rails/arel#98fc259Sean Griffin2014-11-291-1/+1
| | | | | | | | | | `where_sql` now requires that we pass it an engine. None of the manager classes take an engine in their constructor.
* | Pass symbol as an argument instead of a blockErik Michaels-Ober2014-11-294-10/+8
| |
* | Adds preloaded_records method to NullPreloaderThorsten Ball2014-11-281-0/+1
| | | | | | | | | | | | | | | | This fixes a regression where preloading association throws an exception if one of the associations in the preloading hash doesn't exist for one record. Fixes #16070
* | Setting an association replaces records with the same id in memorySean Griffin2014-11-251-2/+18
| | | | | | | | | | | | | | | | | | | | | | The records weren't being replaced since equality in Active Record is defined in terms of `id` only. It is reasonable to expect that the references would be replaced in memory, even if no queries are actually executed. This change did not appear to affect any other parts of the code base. I chose not to execute callbacks since we're not actually modifying the association in a way that will be persisted. Fixes #17730
* | Combine aliased_table_for and aliased_name_foreileencodes2014-11-242-13/+4
| | | | | | | | | | | | | | | | | | This refactoring reduces the number of conditionals needed to build `aliased_table_for` and removes `aliased_name_for` because it's no longer necessary. `aliased_name_for` was also used in `JoinDependency#initialize` so that was replaced with `aliased_table_for` as well.
* | Fix includes on association with a scope containing joins along with conditionssiddharth@vinsol.com2014-11-211-8/+2
| | | | | | | | on the joined assoiciation
* | Wrap code snippets in +, not backticks, in sdocclaudiob2014-11-201-2/+2
| | | | | | | | | | | | | | | | I grepped the source code for code snippets wrapped in backticks in the comments and replaced the backticks with plus signs so they are correctly displayed in the Rails documentation. [ci skip]
* | Merge pull request #17575 from shikshachauhan/make-habtm-consistentRafael Mendonça França2014-11-191-1/+1
|\ \ | | | | | | Allow class_name option in habtm to be consistent with other association...
| * | Allow habtm class_name option to be consistent with other associationsshiksha2014-11-131-1/+1
| | |
* | | Improve the performance of reading attributesSean Griffin2014-11-182-2/+2
| | | | | | | | | | | | | | | | | | | | | We added a comparison to "id", and call to `self.class.primary_key` a *lot*. We also have performance hits from `&block` all over the place. We skip the check in a new method, in order to avoid breaking the behavior of `read_attribute`
* | | Remove the unused second argument to `substitute_at`Sean Griffin2014-11-172-3/+2
| | | | | | | | | | | | Oh hey, we got to remove some code because of that!
* | | Merge pull request #11694 from ↵Rafael Mendonça França2014-11-101-1/+7
|\ \ \ | |/ / |/| | | | | | | | | | | | | | | | | | | | Empact/association-bind-values-not-updated-on-save Fix that a collection proxy could be cached before the save of the owner, resulting in an invalid proxy lacking the owner’s id Conflicts: activerecord/CHANGELOG.md
| * | Fix that a collection proxy could be cached before the save of the owner, ↵Ben Woosley2014-10-031-1/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | resulting in an invalid proxy lacking the owner’s id. Absent this fix calls like: owner.association.update_all to behave unexpectedly because they try to act on association objects where owner_id is null. more evidence here: https://gist.github.com/Empact/5865555 ``` Active Record 3.2.13 -- create_table(:firms, {:force=>true}) -> 0.1371s -- create_table(:clients, {:force=>true}) -> 0.0005s 1 clients. 1 expected. 1 clients updated. 1 expected. ``` ``` Active Record 4.0.0 -- create_table(:firms, {:force=>true}) -> 0.1606s -- create_table(:clients, {:force=>true}) -> 0.0004s 1 clients. 1 expected. 0 clients updated. 1 expected. ```
* | | Ensure HABTM relationships produce valid class names (Fixes #17119)Sammy Larbi2014-11-091-1/+1
| | |
* | | default scopes should break the cache on singulur_association.alfa-jpn2014-11-081-1/+2
| | | | | | | | | | | | fixes #17495
* | | default scopes should break the cache on has_many.Aaron Patterson2014-11-071-1/+2
| | | | | | | | | | | | | | | | | | if you specify a default scope on a model, it will break caching. We cannot predict what will happen inside the scope, so play it safe for now. fixes #17495
* | | Merge pull request #17360 from bronzle/includes_and_unscopedYves Senn2014-11-051-0/+1
|\ \ \ | | | | | | | | | | | | copy reflection_scopes’s unscoped value when building scope for preloading
| * | | copy reflection_scopes’s unscoped value when building scope for ↵Byron Bischoff2014-10-221-0/+4
| | | | | | | | | | | | | | | | preloading, fixes #11036
* | | | Avoid unnecessary allocations/callsPablo Herrero2014-11-021-1/+1
| | | |
* | | | Use bind values for joined tables in where statementsSean Griffin2014-11-011-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In practical terms, this allows serialized columns and tz aware columns to be used in wheres that go through joins, where they previously would not behave correctly. Internally, this removes 1/3 of the cases where we rely on Arel to perform type casting for us. There were two non-obvious changes required for this. `update_all` on relation was merging its bind values with arel's in the wrong order. Additionally, through associations were assuming there would be no bind parameters in the preloader (presumably because the where would always be part of a join) [Melanie Gilman & Sean Griffin]
* | | | edit pass over all warningsXavier Noria2014-10-281-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch uniformizes warning messages. I used the most common style already present in the code base: * Capitalize the first word. * End the message with a full stop. * "Rails 5" instead of "Rails 5.0". * Backticks for method names and inline code. Also, converted a few long strings into the new heredoc convention.
* | | | let's warn with heredocsXavier Noria2014-10-281-5/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The current style for warning messages without newlines uses concatenation of string literals with manual trailing spaces where needed. Heredocs have better readability, and with `squish` we can still produce a single line. This is a similar use case to the one that motivated defining `strip_heredoc`, heredocs are super clean.
* | | | Prefix internal method with _Rafael Mendonça França2014-10-251-1/+1
|/ / / | | | | | | | | | This will avoid naming clash with user defined methods
* | | make sure cache is not used for collection assocations tooAaron Patterson2014-10-141-1/+6
| | | | | | | | | | | | follow up for #17052
* | | break cache if we're inside a "scoping" call. fixes #17052Aaron Patterson2014-10-141-1/+6
| | | | | | | | | | | | | | | For now, we don't want to take "scoping" calls in to account when calculating cache keys for relations, so just opt-out.
* | | measure record instantiation time in AS::NotificationsAaron Patterson2014-10-131-3/+12
| | | | | | | | | | | | | | | emit an event when we instantiate AR objects so we can see how many records were instantiated and how long it took
* | | Merge pull request #17146 from divineforest/active-record-gsub-to-trRafael Mendonça França2014-10-071-1/+1
|\ \ \ | | | | | | | | Change `gsub` to `tr` where possible
| * | | Change `gsub` to `tr` where possibleAlexander Balashov2014-10-061-1/+1
| |/ /
* / / [ci skip] Clarify deletion strategies for collection proxieseileencodes2014-10-041-17/+14
|/ / | | | | | | | | | | | | | | | | | | | | For detailed testing of behavior see: https://gist.github.com/eileencodes/5b0a2fe011dcff6203fe This shows destroy_all always destroys records and fires callbacks. It will never use nullify or delete_all delete_all's behavior varies greatly based on `hm` vs `hm:t` and deletion strategy.
* | Merge pull request #17093 from ↵Aaron Patterson2014-09-291-1/+1
|\ \ | | | | | | | | | | | | phiggins/remove-dynamic-send-on-built-in-callbacks Reduce allocations when running AR callbacks.
| * | Reduce allocations when running AR callbacks.Pete Higgins2014-09-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Inspired by @tenderlove's work in c363fff29f060e6a2effe1e4bb2c4dd4cd805d6e, this reduces the number of strings allocated when running callbacks for ActiveRecord instances. I measured that using this script: ``` require 'objspace' require 'active_record' require 'allocation_tracer' ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:" ActiveRecord::Base.connection.instance_eval do create_table(:articles) { |t| t.string :name } end class Article < ActiveRecord::Base; end a = Article.create name: "foo" a = Article.find a.id N = 10 result = ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end result.sort.each do |k,v| p k => v end puts "total: #{result.values.map(&:first).inject(:+)}" ``` When I run this against master and this branch I get this output: ``` pete@balloon:~/projects/rails/activerecord$ git checkout master M Gemfile Switched to branch 'master' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_before pete@balloon:~/projects/rails/activerecord$ git checkout remove-dynamic-send-on-built-in-callbacks M Gemfile Switched to branch 'remove-dynamic-send-on-built-in-callbacks' pete@balloon:~/projects/rails/activerecord$ bundle exec ruby benchmark_allocation_with_callback_send.rb > allocations_after pete@balloon:~/projects/rails/activerecord$ diff allocations_before allocations_after 39d38 < {["/home/pete/projects/rails/activesupport/lib/active_support/callbacks.rb", 81]=>[40, 0, 0, 0, 0, 0]} 42c41 < total: 630 --- > total: 590 ``` In addition to this, there are two micro-optimizations present: * Using `block.call if block` vs `yield if block_given?` when the block was being captured already. ``` pete@balloon:~/projects$ cat benchmark_block_call_vs_yield.rb require 'benchmark/ips' def block_capture_with_yield &block yield if block_given? end def block_capture_with_call &block block.call if block end def no_block_capture yield if block_given? end Benchmark.ips do |b| b.report("block_capture_with_yield") { block_capture_with_yield } b.report("block_capture_with_call") { block_capture_with_call } b.report("no_block_capture") { no_block_capture } end pete@balloon:~/projects$ ruby benchmark_block_call_vs_yield.rb Calculating ------------------------------------- block_capture_with_yield 124979 i/100ms block_capture_with_call 138340 i/100ms no_block_capture 136827 i/100ms ------------------------------------------------- block_capture_with_yield 5703108.9 (±2.4%) i/s - 28495212 in 4.999368s block_capture_with_call 6840730.5 (±3.6%) i/s - 34169980 in 5.002649s no_block_capture 5821141.4 (±2.8%) i/s - 29144151 in 5.010580s ``` * Defining and calling methods instead of using send. ``` pete@balloon:~/projects$ cat benchmark_method_call_vs_send.rb require 'benchmark/ips' class Foo def tacos nil end end my_foo = Foo.new Benchmark.ips do |b| b.report('send') { my_foo.send('tacos') } b.report('call') { my_foo.tacos } end pete@balloon:~/projects$ ruby benchmark_method_call_vs_send.rb Calculating ------------------------------------- send 97736 i/100ms call 151142 i/100ms ------------------------------------------------- send 2683730.3 (±2.8%) i/s - 13487568 in 5.029763s call 8005963.9 (±2.7%) i/s - 40052630 in 5.006604s ``` The result of this is making typical ActiveRecord operations slightly faster: https://gist.github.com/phiggins/e46e51dcc7edb45b5f98
* | | Remove defunct ivarsBen Woosley2014-09-281-1/+0
|/ / | | | | | | @column_names_with_alias, @dynamic_methods_hash, @time_zone_column_names, and @cached_time_zone
* | Merge pull request #16379 from JackDanger/update-preloader-documentationRafael Mendonça França2014-09-101-23/+32
|\ \ | | | | | | Updating Associations::Preloader docs
| * | Updating Associations::Preloader docsJack Danger Canty2014-08-171-23/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | Much of the previous documentation introduced features new in 2011. This commit refreshes it to provide clearer code examples and spends more time describing the normal case (preloaded associations) and less time describing the fallback. [ci skip]
* | | Fixed regression with referencing polymorphic assoc in eager-loadGodfrey Chan2014-09-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is cased by 03118bc + 9b5d603. The first commit referenced the undefined local variable `column` when it should be using `reflection.type` as the lookup key. The second commit changed `build_arel` to not modify the `bind_values` in- place so we need to combine the arel's `bind_values` with the relation's when building the SQL. Fixes #16591 Related #15821 / #15892 / 7aeca50