aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/models
Commit message (Collapse)AuthorAgeFilesLines
* Merge pull request #23146 from piotrj/issue_18424Ryuta Kamizono2018-01-111-0/+3
|\ | | | | | | When deleting through records, take into account association conditions
| * When deleting through records, take into account association conditionsPiotr Jakubowski2016-05-041-0/+3
| | | | | | | | | | | | | | | | Fixes #18424. When deleting through records, it didn't take into account the conditions that may have been affecting join model table, but was defined in association definition.
* | Fix newly added reflection order when redefining associationRyuta Kamizono2018-01-041-2/+2
| | | | | | | | | | | | | | | | | | | | Currently reflections keeps the order when first added even if when redefining association. As a result of the order, redefining through association which use newly added association will raise `HasManyThroughOrderError`. We need to redefine reflection order as well when redefining association. Fixes #31068.
* | Merge pull request #27561 from fishbrain/count-all-in-has-many-associationRyuta Kamizono2018-01-031-0/+2
|\ \ | | | | | | | | | Use `count(:all)` in HasManyAssociation#count_records
| * | Use `count(:all)` in HasManyAssociation#count_recordsKlas Eskilson2017-02-071-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Problem: Calling `count` on an association can cause invalid SQL queries to be created where the `SELECT COUNT(a, b, c)` function receives multiple columns. This will cause a `StatementInvalid` exception later on. Solution: Use `count(:all)`, which generates a `SELECT COUNT(*)...` query independently of the association. This also includes a test case that, before the fix, broke.
* | | Add test case for `collection_singular_ids` with symbol primary keysRyuta Kamizono2018-01-011-1/+1
| | | | | | | | | | | | This is a regression test for #27864.
* | | save attributes changed by callbacks after update_attributeMike Busch2017-12-221-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | update_attribute previously stopped execution, before saving and before running callbacks, if the record's attributes hadn't changed. [The documentation](http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute) says that "Callbacks are invoked", which was not happening if the persisted attributes hadn't changed.
* | | Fix conflicts `counter_cache` with `touch: true` by optimistic locking.bogdanvlviv2017-12-121-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ``` # create_table :posts do |t| # t.integer :comments_count, default: 0 # t.integer :lock_version # t.timestamps # end class Post < ApplicationRecord end # create_table :comments do |t| # t.belongs_to :post # end class Comment < ApplicationRecord belongs_to :post, touch: true, counter_cache: true end ``` Before: ``` post = Post.create! # => begin transaction INSERT INTO "posts" ("created_at", "updated_at", "lock_version") VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0) commit transaction comment = Comment.create!(post: post) # => begin transaction INSERT INTO "comments" ("post_id") VALUES (1) UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1, "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1 UPDATE "posts" SET "updated_at" = '2017-12-11 21:27:11.398330', "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0 rollback transaction # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post. Comment.take.destroy! # => begin transaction DELETE FROM "comments" WHERE "comments"."id" = 1 UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1, "lock_version" = COALESCE("lock_version", 0) + 1 WHERE "posts"."id" = 1 UPDATE "posts" SET "updated_at" = '2017-12-11 21:42:47.785901', "lock_version" = 1 WHERE "posts"."id" = 1 AND "posts"."lock_version" = 0 rollback transaction # => ActiveRecord::StaleObjectError: Attempted to touch a stale object: Post. ``` After: ``` post = Post.create! # => begin transaction INSERT INTO "posts" ("created_at", "updated_at", "lock_version") VALUES ("2017-12-11 21:27:11.387397", "2017-12-11 21:27:11.387397", 0) commit transaction comment = Comment.create!(post: post) # => begin transaction INSERT INTO "comments" ("post_id") VALUES (1) UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) + 1, "lock_version" = COALESCE("lock_version", 0) + 1, "updated_at" = '2017-12-11 21:37:09.802642' WHERE "posts"."id" = 1 commit transaction comment.destroy! # => begin transaction DELETE FROM "comments" WHERE "comments"."id" = 1 UPDATE "posts" SET "comments_count" = COALESCE("comments_count", 0) - 1, "lock_version" = COALESCE("lock_version", 0) + 1, "updated_at" = '2017-12-11 21:39:02.685520' WHERE "posts"."id" = 1 commit transaction ``` Fixes #31199.
* | | Inverse instance should not be reloaded during autosave if called in validationAnmol Chopra2017-11-271-0/+4
| | | | | | | | | | | | | | | Record saved in save_has_one_association already make call to association.loaded! via record's before_save callback of save_belongs_to_association, but this will reload object if accessed in record's validation.
* | | try using regexesBen Toews2017-11-097-21/+17
| | |
* | | allow table name and direction in string order argBen Toews2017-11-099-25/+29
| | |
* | | work around deprecation warnings in a bunch of testsBen Toews2017-11-0914-42/+46
| | |
* | | `scoping` should respect current class and STI constraint (#29199)Ryuta Kamizono2017-11-061-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | A relation includes `klass`, so it can not be used as it is if current class is different from `current_scope.klass`. It should be created new relation by current class to respect the klass and STI constraint. Fixes #17603. Fixes #23576.
* | | delegate scope forpavel2017-10-274-0/+5
| | |
* | | Place `MocktailDesigner` in `test/models/drink_designer.rb`Ryuta Kamizono2017-10-232-4/+3
| | | | | | | | | | | | | | | Since `MocktailDesigner` inherits `DrinkDesigner` and can not be used alone.
* | | Convert ignored_columns to a list of stringAltech2017-10-201-0/+8
| | |
* | | Test `ignored_columns` value is inheritable by subclassesRyuta Kamizono2017-10-191-0/+3
| | |
* | | Joined tables in association scope doesn't use the same aliases with the ↵Ryuta Kamizono2017-10-091-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | parent relation's aliases Building association scope in join dependency should respect the parent relation's aliases to avoid using the same alias name more than once. Fixes #30681.
* | | Add test case for `arel_attribute` with a custom tableRyuta Kamizono2017-09-271-1/+1
| | | | | | | | | | | | Since #29301, `arel_attribute` respects a custom table name.
* | | `has_many :through` with unscope should affect to through scopeRyuta Kamizono2017-09-071-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | The order of scope evaluation should be from through scope to the association's own scope. Otherwise the association's scope cannot affect to through scope. Fixes #13677. Closes #28449.
* | | Scope in associations should treat nil as `all`Ryuta Kamizono2017-09-041-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | Defined scope treats nil as `all`, but scope in associations isn't so. If the result of the scope is nil, most features on associations will be broken. It should treat nil as `all` like defined scope. Fixes #20823.
* | | `values[:includes]` in `reflection_scope` is not compatible with `through_scope`Ryuta Kamizono2017-09-021-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, preloading `:comments_with_include` will cause the following error: ``` % bundle exec ruby -w -Itest test/cases/associations/eager_test.rb -n test_eager_with_has_many_through_join_model_with_include Using sqlite3 Run options: -n test_eager_with_has_many_through_join_model_with_include --seed 1502 E Error: EagerAssociationTest#test_eager_with_has_many_through_join_model_with_include: ActiveRecord::AssociationNotFoundError: Association named 'post' was not found on Post; perhaps you misspelled it? ```
* | | `counter_cache` requires association class before `attr_readonly`Yasuo Honda2017-08-231-0/+3
| | | | | | | | | | | | | | | There were similar pull requests #26370 #27575 fixed by different way by moving `require "models/post"` before `require "models/comment"`
* | | Clarify base_class tests on abstract STI vs concrete STIYukio Mizuta2017-08-121-1/+6
| | |
* | | Merge pull request #29848 from kamipo/fix_distinct_count_with_order_and_limitRafael França2017-07-242-34/+35
|\ \ \ | | | | | | | | Fix `COUNT(DISTINCT ...)` with `ORDER BY` and `LIMIT`
| * | | Extract `Account` model to the dedicated fileRyuta Kamizono2017-07-222-34/+35
| | | |
* | | | Merge pull request #29765 from lugray/fix_counter_cacheRafael França2017-07-241-0/+6
|\ \ \ \ | |/ / / |/| | | Fix `counter_cache` double increment
| * | | Add test for fixed `counter_cache` double incrementLisa Ugray2017-07-191-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When an `after_create` callback did `update_attributes` on a record with multiple `belongs_to` associations with counter caches, even numbered associations would have their counters double-incremented. Fixes to `ActiveModel::Dirty` in 020abad fixed this. This adds regression tests for this bug fixed incidentally in the other commit, which also removed the need for the workaround using @_after_create_counter_called.
* | | | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-19144-10/+298
|/ / /
* | | Fix unscoping `default_scope` in STI associationsRyuta Kamizono2017-07-191-0/+1
| | | | | | | | | | | | | | | | | | | | | Since 5c71000, it has lost to be able to unscope `default_scope` in STI associations. This change will use `.empty_scope?` instead of `.values.empty?` to regard as an empty scope if only have `type_condition`.
* | | Remove deprecated code concerning non-attributes and `*_will_change!`Sean Griffin2017-07-181-1/+1
| | |
* | | Merge pull request #29679 from kamipo/add_test_case_for_27724Kasper Timm Hansen2017-07-151-0/+5
|\ \ \ | | | | | | | | Add a test case for overwriting existing condition on associations
| * | | Add a test case for overwriting existing condition on associationsRyuta Kamizono2017-07-071-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Overwriting existing condition on associations has already supported (23bcc65 for eager loading, 2bfa2c0 for preloading). Fixes #27724. Closes #29154.
* | | | Extract `FakeKlass` in `relation_test.rb` and `relation/mutation_test.rb`Ryuta Kamizono2017-07-111-0/+32
|/ / / | | | | | | | | | | | | `FakeKlass` in `relation_test.rb` and `relation/mutation_test.rb` are almost the same.
* | | Fix preloading association with scope including joinsRyuta Kamizono2017-07-042-0/+3
| | |
* | | Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-02144-154/+10
| | | | | | | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* | | Enforce frozen string in RubocopKir Shatrov2017-07-01144-10/+154
| | |
* | | Merge pull request #29416 from kamipo/remove_unused_subject_modelRafael França2017-06-281-14/+0
|\ \ \ | | | | | | | | Remove unused `Subject` model in tests
| * | | Remove unused `Subject` model in testsRyuta Kamizono2017-06-111-14/+0
| | | | | | | | | | | | | | | | Because `subjects` table doesn't exist.
* | | | Merge pull request #29405 from kamipo/locked_should_not_build_arelRafael França2017-06-281-0/+1
|\ \ \ \ | | | | | | | | | | `Relation#locked?` should not build arel
| * | | | Remove delegating to arel in a relationRyuta Kamizono2017-06-291-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | The delegation was needed since passing `relation` with `relation.bound_attributes`. It should use `relation.arel` in that case.
* | | | | Ensure that using correct alias trackerRyuta Kamizono2017-06-251-0/+1
|/ / / / | | | | | | | | | | | | | | | | | | | | Covering #27994 in tests. Closes #27994.
* / / / Remove unused defined associationRyuta Kamizono2017-06-111-1/+0
|/ / / | | | | | | | | | | | | `belongs_to :developer` on `Comment` model was added in 431f8e0 but it is unused.
* | | Correct a has_many association testKoichi ITO2017-06-011-0/+1
| | |
* | | Merge pull request #26634 from kamipo/extract_numeric_dataRafael França2017-05-311-0/+8
|\ \ \ | | | | | | | | Extract `NumericData` model for tests
| * | | Restore the override of numeric attributes properlyRyuta Kamizono2016-12-221-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | `attribute :world_population, :integer` is not a same with default decimal without scale type unless #26302 is merged. Should be `attribute :world_population, :big_integer` for now.
| * | | Extract `NumericData` model for testsRyuta Kamizono2016-09-271-0/+3
| | | | | | | | | | | | | | | | Currently `NumericData` model is defined some places.
* | | | Add missing `delegate :extending, to: :all`Ryuta Kamizono2017-06-011-0/+2
| | | |
* | | | Merge pull request #29098 from kamipo/fix_association_with_extension_issuesMatthew Draper2017-05-302-1/+11
|\ \ \ \ | | | | | | | | | | | | | | | Fix association with extension issues
| * | | | Fix association with extension issuesRyuta Kamizono2017-05-282-1/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This fixes the following issues. * `association_scope` doesn't include `default_scope`. Should use `scope` instead. * We can't use `method_missing` for customizing existing method. * We can't use `relation_delegate_class` for sharing extensions. Should extend per association.