aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #12071 from Crunch09/outer_joinsSean Griffin2015-10-304-5/+56
|\ | | | | | | added ActiveRecord::Relation#outer_joins
| * added ActiveRecord::Relation#left_outer_joinsFlorian Thomas2015-05-194-5/+56
| | | | | | | | | | | | Example: User.left_outer_joins(:posts) => SELECT "users".* FROM "users" LEFT OUTER JOIN "posts" ON "posts"."user_id" = "users"."id"
* | Ensure `has_and_belongs_to_many` works with `belongs_to_required_by_default`Sean Griffin2015-10-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | Before this commit, if `ActiveRecord::Base.belongs_to_required_by_default` is set to `true`, then creating a record through `has_and_belongs_to_many` fails with the cryptic error message `Left side must exist`. This is because `inverse_of` isn't working properly in this case, presumably since we're doing trickery with anonymous classes in the middle. Rather than following this rabbit hole to try and get `inverse_of` to work in a case that we know is not publicly supported, we can just turn off this validation to match the behavior of 4.2 and earlier.
* | Merge pull request #13008 from ktheory/sanitize_orderSean Griffin2015-10-291-0/+7
|\ \ | | | | | | | | | Support SQL sanitization in AR::QueryMethods#order
| * | Support SQL sanitization in AR::QueryMethods#orderAaron Suggs2013-11-221-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Add support for sanitizing arrays in SQL ORDER clauses. This is useful when using MySQL `ORDER BY FIELD()` to return records in a predetermined way. ```ruby Tag.order(['field(id, ?', [1,3,2]].to_sql # => SELECT "tags".* FROM "tags" ORDER BY field(id, 1,3,2) ``` Prior to this, developers must be careful to sanitize `#order` arguments themselves.
* | | Merge pull request #19511 from larskanis/replace_const_conn_paramsSean Griffin2015-10-291-7/+2
|\ \ \ | | | | | | | | PostgreSQL, Replace static connection param list by libpq's dynamic list
| * | | PostgreSQL, Replace static connection param list by the one built into libpq.Lars Kanis2015-03-251-7/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This makes the connection adapter future-proof regarding to new parameters. To maintain backward compatibility, :requiressl is added by hand. It is deprecated by PostgreSQL since 2003, but still accepted by libpq.
* | | | Don't disable errors when turning standard_conforming_strings onHarry Marr2015-10-291-7/+2
| | | |
* | | | Never pass `nil` to `order`Sean Griffin2015-10-294-16/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is part of a refactoring to make it easier to allow `order` to use sanitize like just about everything else on relation. The deleted test doesn't give any reasoning as to why passing `nil` to `order` needs to be supported, and it's rather nonsensical. I can almost see allowing an empty string being passed (though I'm tempted to just disallow it...)
* | | | Merge pull request #22106 from jamis/fix_default_fixture_class_namesYves Senn2015-10-291-3/+1
|\ \ \ \ | | | | | | | | | | default value for FixtureSet.fixture_class_names ought to be a Class
| * | | | FixtureSet.fixture_class_names should have no default valueJamis Buck2015-10-291-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Look at `TestFixtures.set_fixture_class`. As documented, it accepts a mapping of fixture identifiers (string or symbol) to Classes (the model classes that implement the named fixture). Look now at the initialization of `TestFixtures.fixture_class_names`. It defines a Hash, which will return a string by default (where the string is the estimated class name of the given fixture identifier). Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`, passing in the mapping of `fixture_class_names`. Following this on to `FixtureSet.create_fixtures`, this instantiates a `FixtureSet::ClassCache`, passing in the map of class names. `ClassCache`, in turn, calls `insert_class` for each value in the cache. (Recall that `set_fixture_class` puts Class objects in there, while the default proc for the mapping puts String objects.) Look finally at `insert_class`. If the value is present, it checks to see if the value is a subclass of `AR::Base`. Fair enough...but wait! What if the value is a String? You get an exception, because a String instance cannot be compared with a Class. Judging from the implementation, it seems like the expected behavior here is for `fixture_class_names` to have no default proc. Look-ups are supposed to happen via `ClassCache`, with `fixture_class_names` existing solely as a repository for explicitly-registered class mappings. That is what this change does.
* | | | | Revert "Revert "Merge pull request #22026 from ↵Yves Senn2015-10-291-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | akihiro17/fix-preload-association"" This reverts commit 5243946017d09afff4d70d273b0fcdfd41a4b22a. This fixes an issue with the build where tests would fail on mysql and postgresql due to different ordering.
* | | | | Merge pull request #22101 from gocardless/ar-pgbouncer-fixAndrew White2015-10-291-4/+5
|\ \ \ \ \ | | | | | | | | | | | | Avoid disabling postgres errors
| * | | | | Check standard_conforming_strings is not readonlyHarry Marr2015-10-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In Postgres 8.1 the standard_conforming_strings setting was read-only, meaning you got an error if you tried to update it. By filtering on `context = 'user'` we only try to update the setting if it's user-writable[1]. [1]: http://www.postgresql.org/docs/9.4/static/view-pg-settings.html
| * | | | | Avoid disabling postgres errorsHarry Marr2015-10-281-4/+5
| |/ / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The standard_conforming_strings setting doesn't exist on all versions of Postgres, but if it does exist, Rails turns it on. Previously this was done by effectively disabling errors on the Postgres connection, issuing a SET to turn the setting on, then re-enabling errors on the connection. However, if you're running pgbouncer in transaction-pooling mode, you can't guarantee that successive calls to `#execute` will be sent to the same pgbouncer-postgres connection, so you can end up disabling errors on a different postgres connection, and never re-enabling them. Future queries on that connection that result in errors (e.g. violating unique constraints) will leave the connection in a bad state where successive queries will fail. This commit sets standard_conforming_strings by issuing an UPDATE to pg_settings, which will update the setting if it exists, and do nothing if it doesn't (rather than erroring out like SET would), which means we can remove the error-disabling code. It's also worth noting that Postgres has allowed standard_conforming_strings to be updated since 8.2 (which is the oldest version Rails supports), so technically we probably don't even need to be defensive here.
* | | | | Revert "Merge pull request #22026 from akihiro17/fix-preload-association"Sean Griffin2015-10-291-4/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit 6dc6a0b17cfaf7cb6aa2b1c163b6ca141b538a8e, reversing changes made to ec94f00ba3cf250eb54fc5b7a5e3ed4b90164f34. This pull request broke the build.
* | | | | Merge pull request #19501 from ccutrer/dry_sti_subclass_finding2Sean Griffin2015-10-291-20/+18
|\ \ \ \ \ | | | | | | | | | | | | | | | | | | DRY up STI subclass logic
| * | | | | Fix a stylistic nitpick in #19501Sean Griffin2015-10-291-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We don't need to use `String#+` or create all the intermediate strings to break a string into multiple lines. We can just write a c-style multiline string literal. This is by no means a hotpath, but this is clearer to me anyway.
| * | | | | Fix test failures caused by #19501Sean Griffin2015-10-291-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The first one is quite straightforward. We want to give the proper error message in the case where a top level constant exists, but we're looking for a nested one. We just need to port over the change to use `subclass.name` into these changes. The second set of failures, which are only present in the mysql adapter tests, are stranger to me. The failure occurs because we were previously comparing `subclass.name == self.name` instead of `subclass == self`. However, I don't think that we need to support creating anonymous classes which share a table with a class that uses STI, overrides `name` to return the same name as athe class that we have no other relationship with, when not assigned to a constant so it could never be used anyway... The commits around why that exist give no context, and I think they're just poorly written tests (WTF does `test_schema` mean anyway, and why does calling `.first` on some anonymous class test it?). We'll just disable STI on that class.
| * | | | | Fix merge conflicts from #19501Sean Griffin2015-10-291-20/+18
| |\ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I'm making this commit separately because this has failing tests and style nitpicks that I'd like to make as individual commits, to make the changes I'm making explicit. We still want a single merge commit at the end, however.
| | * | | | | DRY up STI subclass logicCody Cutrer2015-03-241-19/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | the newer method used for discriminating new records did not use the older and more robust method used for instantiating existing records, but did have a better post-check to ensure the sublass was in the hierarchy. so move the descendants check to find_sti_class, and then simply call find_sti_class from subclass_from_attributes now with fixed specs
* | | | | | | Set `scope.reordering_value` to `true` if :reordering values are specifiedakihiro172015-10-301-0/+4
|/ / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We should call `scope.order!` and set `scope.reordering_value` to `true` if :reordering values are specified Fixes #21886
* | | | | | Merge pull request #18548 from ↵Sean Griffin2015-10-281-0/+5
|\ \ \ \ \ \ | |_|/ / / / |/| | | | | | | | | | | | | | | | | | | | | | | sebjacobs/support-bidirectional-destroy-dependencies Add support for bidirectional destroy dependencies
| * | | | | Add support for bidirectional destroy dependenciesSeb Jacobs2015-01-161-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Prior to this commit if you defined a bidirectional relationship between two models with destroy dependencies on both sides, a call to `destroy` would result in an infinite callback loop. Take the following relationship. class Content < ActiveRecord::Base has_one :content_position, dependent: :destroy end class ContentPosition < ActiveRecord::Base belongs_to :content, dependent: :destroy end Calling `Content#destroy` or `ContentPosition#destroy` would result in an infinite callback loop. This commit changes the behaviour of `ActiveRecord::Callbacks#destroy` so that it guards against subsequent callbacks. Thanks to @zetter for demonstrating the issue with failing tests[1]. [1] rails#13609
* | | | | | Fix Typo in EagerLoadPolymorphicError [ci skip]Alex Myers2015-10-271-1/+1
| | | | | |
* | | | | | Merge pull request #18383 from ↵Rafael Mendonça França2015-10-271-1/+2
|\ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | scambra/habtm-with-where-includes-16032-for-master Includes HABTM returns correct size now
| * | | | | | Includes HABTM returns correct size now. It's caused by the join dependencySergio Cambra2015-01-071-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | only instantiates one HABTM object because the join table hasn't a primary key. Updated commit from @bigxiang commit dbaa837 Fixes #16032. Examples: before: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 1 after: Project.first.salaried_developers.size # => 3 Project.includes(:salaried_developers).first.salaried_developers.size # => 3
* | | | | | | fix typo in `db:purge` description [ci skip]yuuji.yaginuma2015-10-281-1/+1
| | | | | | |
* | | | | | | [ci skip] Add doc for preloader_forschneems2015-10-261-0/+4
| | | | | | |
* | | | | | | [ci skip] Clarify doc for preloaders_for_oneschneems2015-10-261-0/+5
| | | | | | |
* | | | | | | [ci skip] Add doc to preloaders_onschneems2015-10-261-0/+1
| | | | | | |
* | | | | | | Merge pull request #19686 from tsun1215/index_errorsSean Griffin2015-10-262-4/+10
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Errors can be indexed with nested attributes Close #8638
| * | | | | | | Errors can be indexed with nested attributesMichael Probber2015-04-172-4/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `has_many` can now take `index_errors: true` as an option. When this is enabled, errors for nested models will be returned alongside an index, as opposed to just the nested model name. This option can also be enabled (or disabled) globally through `ActiveRecord::Base.index_nested_attribute_errors` E.X. ```ruby class Guitar < ActiveRecord::Base has_many :tuning_pegs accepts_nested_attributes_for :tuning_pegs end class TuningPeg < ActiveRecord::Base belongs_to :guitar validates_numericality_of :pitch end ``` - Old style - `guitar.errors["tuning_pegs.pitch"] = ["is not a number"]` - New style (if defined globally, or set in has_many_relationship) - `guitar.errors["tuning_pegs[1].pitch"] = ["is not a number"]` [Michael Probber, Terence Sun]
* | | | | | | | Merge pull request #19924 from iamvery/db-tasks-exit-statusSean Griffin2015-10-262-3/+9
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | Explicitly exit with status "1" for create and drop failures
| * | | | | | | | Exit with non-zero status when db:drop failsJay Hayes2015-10-201-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * If the drop task fails for a reason other than the database not existing, processing should end. This is indicated by a non-zero exit status. * Since the backtrace is already printed to screen, we forgo printing it again by using an explicit call to `exit`. * :warning: This modifies the behavior of the db:create task slightly in that the stack trace is no longer printed by default. If the `--trace` option is used, it will print the trace _after_ the error message.
| * | | | | | | | Exit with non-zero status when db:create failsJay Hayes2015-10-201-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * If the create task fails for a reason other than the database already existing, processing should end. This is indicated by a non-zero exit status. * Since the backtrace is already printed to screen, we forgo printing it again by using an explicit call to `exit`. * :warning: This modifies the behavior of the db:create task slightly in that the stack trace is no longer printed by default. If the `--trace` option is used, it will print the trace _after_ the error message.
| * | | | | | | | Fix test of drop failureJay Hayes2015-10-201-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Previously the sqlite3 adapter could not "fail" on drop. Now an error is raised when no file exists. * Also updates purge to be resilient of drop failures. This is how purge is expected to behave.
* | | | | | | | | [ci skip] Fix method documentation locationschneems2015-10-261-3/+2
| | | | | | | | |
* | | | | | | | | Do not omit parentheses [ci skip]Ryuta Kamizono2015-10-253-13/+13
| | | | | | | | |
* | | | | | | | | Stop aligning the argumentsRafael Mendonça França2015-10-231-4/+4
| | | | | | | | |
* | | | | | | | | Use thor class_option to make the primary_key_type option workRafael Mendonça França2015-10-234-10/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Also move the method to the right class
* | | | | | | | | Move default uuid generation to active_recordJon McCartie2015-10-232-1/+9
| | | | | | | | |
* | | | | | | | | Remove no need `binds.empty?` checkingRyuta Kamizono2015-10-231-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `#exec_stmt` is private method and only called in `#exec_query`. it means `binds` is provided always. No need `binds.empty?` checking.
* | | | | | | | | Merge pull request #21918 from bogdan/refactor-preloaderSean Griffin2015-10-221-42/+30
|\ \ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | Refactored association preloader for performance
| * | | | | | | | | Refactored association preloader for performanceBogdan Gusiev2015-10-221-42/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * less arrays created * less complexity with only one level of nesting in loop
* | | | | | | | | | Merge pull request #21950 from ↵Andrew White2015-10-221-10/+5
|\ \ \ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | rafaelsales/issues/21922-fix-ar-group-by-attribute-lookup Fix generated projection fields in group by query
| * | | | | | | | | | Refactor Calculations#execute_grouped_calculation and clean AR test caseRafael Sales2015-10-221-10/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * When tried to use `Company#accounts` test/models/company.rb I got: ``` ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: accounts.company_id: SELECT COUNT(*) AS count_all, "companies"."firm_id" AS companies_firm_id FROM "companies" INNER JOIN "accounts" ON "accounts"."company_id" = "companies"."id" GROUP BY "companies"."firm_id" ``` * The refactor on Calculations class was just to simplify the code
| * | | | | | | | | | Fix generated projection fields in group by queryRafael Sales2015-10-221-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Closes #21922 Let `Book(id, author_id)`, `Photo(id, book_id, author_id)` and `Author(id)` Running `Book.group(:author_id).joins(:photos).count` will produce: * Rails 4.2 - conflicts `author_id` in both projection and group by: ```sql SELECT COUNT(*) AS count_all, author_id AS author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY author_id ``` * Master (9d02a25) - conflicts `author_id` only in projection: ```sql SELECT COUNT(*) AS count_all, author_id AS author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY "books"."author_id" ``` * With this fix: ```sql SELECT COUNT(*) AS count_all, "books"."author_id" AS books_author_id FROM "books" INNER JOIN "photos" ON "photos"."book_id" = "books"."id" GROUP BY "books"."author_id" ```
* | | | | | | | | | | Remove `#tables` extra args againRyuta Kamizono2015-10-222-20/+11
|/ / / / / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This issue was resolved by #21687 already. But re-add args by #18856. `#tables` extra args was only using by `#table_exists?`. This is for internal API. This commit will remove these extra args again.
* | | | | | | | | | Extract native getter to attr_reader.jbranchaud2015-10-211-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The getter is doing nothing more than returning the ivar, so it can be extracted to an attr_reader.