Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, fixtures and default_scope

I have a fixture for the model "SoftwareVersion":

testing:
  id: 4
  version: "4.0"
  file_name: MyString4
  is_testing: true

And a default scope for the model:

 default_scope where(is_testing: false)

If I do this in a test:

software_versions(:testing)

I get an error:

ActiveRecord::RecordNotFound: Couldn't find SoftwareVersion with id=4 [WHERE `software_versions`.`is_testing` = 0]

Is that intended behavior? And how do I override it?

like image 616
jriff Avatar asked Jul 29 '13 10:07

jriff


1 Answers

Yes this is intended behavir, Default scope will affect all queries. Use unscoped to start a clean query again:

SoftwareVersion.unscoped.where(is_testing: true)
SoftwareVersion.unscoped.find(id: 4)
etc.

But that apart, if you're using the same table (with fixtures) for storing both Production and Testing records, then you're doing it wrong. Rails uses a totally different database for testing, so you don't need a column to distinguish. And if you put the fixtures under test/fixtures, they will never get loaded in production at all.

like image 199
Subhas Avatar answered Sep 20 '22 07:09

Subhas