aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Use type column first in multi-column indexesDerek Prior2014-10-242-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | `add_reference` can very helpfully add a multi-column index when you use it to add a polymorphic reference. However, the first column in the index is the `id` column, which is less than ideal. The [PostgreSQL docs][1] say: > A multicolumn B-tree index can be used with query conditions that > involve any subset of the index's columns, but the index is most > efficient when there are constraints on the leading (leftmost) > columns. The [MySQL docs][2] say: > MySQL can use multiple-column indexes for queries that test all the > columns in the index, or queries that test just the first column, the > first two columns, the first three columns, and so on. If you specify > the columns in the right order in the index definition, a single > composite index can speed up several kinds of queries on the same > table. In a polymorphic relationship, the type column is much more likely to be useful as the first column in an index than the id column. That is, I'm more likely to query on type without an id than I am to query on id without a type. [1]: http://www.postgresql.org/docs/9.3/static/indexes-multicolumn.html [2]: http://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
* Remove duplicate 'select' database statementclaudiob2014-10-205-17/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `select` method has the same definition in almost all database adapters, so it can be moved from the database-specific adapters (PostgreSQl, MySQL, SQLite) to the abstract `database_statement`: ```ruby def select(sql, name = nil, binds = []) exec_query(sql, name, binds) end ``` --- More details about this commit: the only two DB-specific adapters that have a different definition of `select` are MySQLAdapter and MySQL2Adapter. In MySQLAdapter, `select` invokes `exec_query(sql, name, binds)`, so calling `super` achieves the same goal with less repetition. In MySQL2Adapter, `select` invokes `exec_query(sql, name)`, that is, it does not pass the `binds` parameter like other methods do. However, [MySQL2Adapter's `exec_query`](https://github.com/rails/rails/blob/74a527cc63ef56f3d0a42cf638299958dc7cb08c/activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb#L228L231) works exactly the same whether this parameters is passed or not, so the output does not change: ```ruby def exec_query(sql, name = 'SQL', binds = []) result = execute(sql, name) ActiveRecord::Result.new(result.fields, result.to_a) end ```
* Merge pull request #17019 from yuki24/add-class-name-to-unknown-attr-errorYves Senn2014-10-201-1/+1
|\ | | | | | | Message on AR::UnknownAttributeError should include the class name of a record
| * AR::UnknownAttributeError should include the class name of a recordYuki Nishijima2014-10-151-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This would be helpful if 2 models have an attribute that has a similar name to the other. e.g: before: User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"]) # => ActiveRecord::UnknownAttributeError: unknown attribute: name after: User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"]) # => ActiveRecord::UnknownAttributeError: unknown attribute on User: name
* | AR::UnknownAttributeError should include the class name of a recordYuki Nishijima2014-10-201-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This would be helpful if 2 models have an attribute that has a similar name to the other. e.g: before: User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"]) # => ActiveRecord::UnknownAttributeError: unknown attribute: name after: User.new(name: "Yuki Nishijima", projects_attributes: [name: "kaminari"]) # => ActiveRecord::UnknownAttributeError: unknown attribute on User: name
* | [ci skip] Make merge method nodocAnshul Sharma2014-10-201-8/+1
| |
* | [ci skip] merge docsAnshul Sharma2014-10-201-0/+7
| |
* | Add a deprecation warning for abiguous boolean valuesSean Griffin2014-10-161-1/+8
| | | | | | | | | | | | | | | | | | | | | | In Rails 5.0, we'd like to change the behavior of boolean columns in Rails to be closer to Ruby's semantics. Currently we have a small set of values which are "truthy", and all others are "falsy". In Rails 5.0, we will reverse this to have a small number of values which are "falsy", and all others will become "truthy". In the interim, all values which are ambiguous must emit a deprecation warning.
* | test, better describe `SerializationTypeMismatch` behavior. refs #14716.Yves Senn2014-10-161-2/+2
| |
* | docs, since #16702 we detect mutation on serialized attributes. [ci skip]Yves Senn2014-10-161-3/+0
| | | | | | | | /cc @sgrif
* | we don't need a HWIA and a hash allocated for just one k/v pairAaron Patterson2014-10-151-5/+3
| |
* | just look up the primary key from the columns hashAaron Patterson2014-10-151-6/+1
| |
* | add table.bigint supportAaron Patterson2014-10-152-1/+2
|/ | | | | | | | In the DSL you can now do: create_table(:foos) do |t| t.bigint :hi end
* Use if/else instead of early raiseRafael Mendonça França2014-10-151-2/+5
|
* Merge pull request #17267 from rebyn/masterRafael Mendonça França2014-10-152-0/+7
|\ | | | | | | Raise an error for has_one associations which try to go :through a polymorphic association
| * Raise an error for has_one associations which try to go :through a ↵Tu Hoang2014-10-152-0/+7
| | | | | | | | polymorphic association [#17263]
* | delete leftover JoinOperation structswapdisc2014-10-141-2/+0
|/
* Make the config actually copyableGodfrey Chan2014-10-141-1/+1
| | | The intention here is to make the required config copy-able from the console/logs, so add a newline at the end of the message to make that easier. (Otherwise it would be `... raise_in_transactional_callbacks = true (called from...`.)
* 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.
* Clarify `#update_all` doc about value processingNicolas Cavigneaux2014-10-141-1/+2
| | | | | | | | | This clarify the fact that `#update_all` doesn't type-cast passed values and that these values are written as-is in the SQL DB. Fix #17242 [ci skip]
* Merge pull request #17251 from claudiob/remove-duplicate-exceptionRafael Mendonça França2014-10-131-6/+10
|\ | | | | Remove duplicate error message "Couldn't find..."
| * Remove duplicate error message "Couldn't find..."claudiob2014-10-131-6/+10
| | | | | | | | | | | | | | | | | | This commit removes the duplication of the error message: > Couldn't find #{@klass.name} with [#{arel.where_sql}] introduced in #15791 by adding a private method `find_nth!` that deals with all the method like `first!` and `second!`.
* | measure record instantiation time in AS::NotificationsAaron Patterson2014-10-132-4/+22
| | | | | | | | | | emit an event when we instantiate AR objects so we can see how many records were instantiated and how long it took
* | add length to ActiveRecord::ResultAaron Patterson2014-10-131-0/+4
| |
* | Autosave callbacks shouldn't be `after_save`Agis-2014-10-131-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | 068f092ced8483e557725542dd919ab7c516e567 registered autosave callbacks as `after_save` callbacks. This caused the regression described in #17209. Autosave callbacks should be registered as `after_update` and `after_create` callbacks, just like before. This is a partial revert of 068f092ced8483e557725542dd919ab7c516e567. Fixes #17209.
* | Merge pull request #17014 from grosser/grosser/fast-fixturesAaron Patterson2014-10-101-34/+2
|\ \ | |/ |/| speed up fixtures by not loading all their classes
| * speed up fixtures by not loading all their classesgrosser2014-10-061-34/+2
| |
* | Merge pull request #17146 from divineforest/active-record-gsub-to-trRafael Mendonça França2014-10-072-2/+2
|\ \ | | | | | | Change `gsub` to `tr` where possible
| * | Change `gsub` to `tr` where possibleAlexander Balashov2014-10-062-2/+2
| | |
* | | [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 #16409 from ↵Zachary Scott2014-10-032-6/+10
|\ \ \ | |/ / |/| | | | | | | | justinweiss/update_validation_context_documentation Docs: Add a note on custom validation contexts. [ci skip]
| * | Add a note on custom validation contexts.Justin Weiss2014-08-052-6/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | The documentation on `:on` for validations was inconsistent, and most only referenced the `:create` and `:update` contexts. I fixed those to be consistent with the documentation on `AM::Validations.validates`, which seemed to have the best docs. [ci skip]
* | | use tr instead of gsub when possibleAdrian Rangel2014-10-011-1/+1
| | |
* | | Merge pull request #17093 from ↵Aaron Patterson2014-09-295-14/+14
|\ \ \ | | | | | | | | | | | | | | | | phiggins/remove-dynamic-send-on-built-in-callbacks Reduce allocations when running AR callbacks.
| * | | Reduce allocations when running AR callbacks.Pete Higgins2014-09-285-14/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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
* | | | Use Hash#each_key instead of Hash#keys.eachErik Michaels-Ober2014-09-292-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | Hash#keys.each allocates an array of keys; Hash#each_key iterates through the keys without allocating a new array. This is the reason why Hash#each_key exists.
* | | | Remove defunct ivarsBen Woosley2014-09-282-11/+7
|/ / / | | | | | | | | | @column_names_with_alias, @dynamic_methods_hash, @time_zone_column_names, and @cached_time_zone
* | | some object allocation reduction for new AR objectsAaron Patterson2014-09-272-3/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Benchmark: ```ruby require 'objspace' require 'active_record' 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 ObjectSpace::AllocationTracer.trace do N.times { Article.find a.id } end ObjectSpace::AllocationTracer.allocated_count_table table.sort_by { |_,x| x }.each do |k,v| p k => (v / N) end ```
* | | Preparing for 4.2.0.beta2 releaseRafael Mendonça França2014-09-261-1/+1
| | |
* | | ar/connection_pool: honor overriden rack.test in middlewareSimon Eskildsen2014-09-231-1/+1
| | | | | | | | | | | | | | | | | | | | | Honoring an overidden `rack.test` allows testing closed connection between multiple requests. This is useful if you're working on database resiliency, to ensure the connection is in the expected state from one request to another on the same worker.
* | | add a truncate method to the connectionAaron Patterson2014-09-223-0/+13
| |/ |/| | | | | | | | | it doesn't work on SQLite3 since it doesn't support truncate, but that's OK. If you call truncate on the connection, you're now bound to that database (same as if you use hstore or any other db specific feature).
* | Clarify the documentation of the primary_key option for associationsPeter Marsh2014-09-221-1/+1
| | | | | | | | | | | | | | Previously the documentation stated that `primary_key` should be the name of a *method* that returns the primary key used for the association. This is incorrect. This changes the documentation to say that the value must be the name of a column.
* | fix fixtures not loading if a belongs_to association is defined with a ↵Will Bryant2014-09-221-1/+1
| | | | | | | | :foreign_key option that's a symbol
* | No need to call to_sym hereGodfrey Chan2014-09-203-10/+10
| | | | | | | | | | The hash is now string-keyed, and [_]reflect_on_association calls `to_s` on the argument anyway.
* | Fix find_by with associations not working with adequate recordGodfrey Chan2014-09-201-0/+3
| | | | | | | | | | | | | | | | | | For now, we will just skip the cache when a non-column key is used in the hash. If the future, we can probably move some of the logic in PredicateBuilder.expand up the chain to make caching possible for association queries. Closes #16903 Fixes #16884
* | Quick fix to address the borken build caused by #15791Godfrey Chan2014-09-201-1/+1
| | | | | | | | | | | | This is pretty lame though, so feel free to send a PR with a better fix! ;) cc @zev @tenderlove
* | Merge pull request #15791 from zev/add_model_to_recordnotfound_messageAaron Patterson2014-09-191-8/+8
|\ \ | | | | | | Update RecordNotFound exception cases to include a message with the
| * | Update RecordNotFound exception cases to include a message with theZev Blut2014-06-181-8/+8
| | | | | | | | | | | | Model that the Record was not found in.
* | | Updated comment about features of fixturesPrathamesh Sonpatki2014-09-191-0/+3
| | | | | | | | | | | | | | | | | | - Added a note about some features which are not dependent on auto-generated ID's. - [ci skip]