diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-11-21 14:57:25 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-11-21 14:57:25 +0100 |
commit | f766abd4cf3eb75469d3646cfb6d85e668c619f3 (patch) | |
tree | ef90237445890a0c5bcb6486179c531e9569f77d | |
parent | 8613b31627fc551dbf637bbe9c5c363f84a3ebea (diff) | |
download | rails-f766abd4cf3eb75469d3646cfb6d85e668c619f3.tar.gz rails-f766abd4cf3eb75469d3646cfb6d85e668c619f3.tar.bz2 rails-f766abd4cf3eb75469d3646cfb6d85e668c619f3.zip |
make it possible to access fixtures excluded by a `default_scope`.
Prior to this patch you'd end up with an error like:
```
ActiveRecord::RecordNotFound: Couldn't find <Model> with 'id'=<id> [WHERE (<default_scope condition>)]
```
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/fixtures.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/fixtures_test.rb | 14 | ||||
-rw-r--r-- | activerecord/test/fixtures/bulbs.yml | 5 |
4 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index e4c59673cd..fcc6523d6e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Make it possible to access fixtures excluded by a `default_scope`. + + *Yves Senn* + * Add `Table#name` to match `TableDefinition#name`. *Cody Cutrer* diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index db6421dacb..660ba18a98 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -806,7 +806,7 @@ module ActiveRecord def find if model_class - model_class.find(fixture[model_class.primary_key]) + model_class.unscoped.find(fixture[model_class.primary_key]) else raise FixtureClassNotFound, "No class attached to find." end diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 4b0ebe13d1..9edeb8b47f 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -5,6 +5,7 @@ require 'models/admin/randomly_named_c1' require 'models/admin/user' require 'models/binary' require 'models/book' +require 'models/bulb' require 'models/category' require 'models/company' require 'models/computer' @@ -853,3 +854,16 @@ class CustomNameForFixtureOrModelTest < ActiveRecord::TestCase assert_equal 'randomly_named_table', Admin::ClassNameThatDoesNotFollowCONVENTIONS.table_name end end + +class FixturesWithDefaultScopeTest < ActiveRecord::TestCase + fixtures :bulbs + + test "inserts fixtures excluded by a default scope" do + assert_equal 1, Bulb.count + assert_equal 2, Bulb.unscoped.count + end + + test "allows access to fixtures excluded by a default scope" do + assert_equal "special", bulbs(:special).name + end +end diff --git a/activerecord/test/fixtures/bulbs.yml b/activerecord/test/fixtures/bulbs.yml new file mode 100644 index 0000000000..e5ce2b796c --- /dev/null +++ b/activerecord/test/fixtures/bulbs.yml @@ -0,0 +1,5 @@ +defaulty: + name: defaulty + +special: + name: special |