Postgres views not recognized by rspec

activerecord, postgresql, rails-activerecord, rspec, ruby-on-rails

Solution

Rails doesn't really understand "advanced" database concepts like views so they won't appear in your `schema.rb`. When rspec is setting up its test database, it will use `schema.rb` to create the database schema, since you won't find your views in `schema.rb`, you won't find your views in the test database that rspec will be using and everything falls apart.

The solution is to switch from `schema.rb` to `structure.sql`. You should be able to update your `config/application.rb` to say:

config.active_record.schema_format = :sql

and then do a `rake db:structure:dump` to generate the `structure.sql` file. Once you have that, remove `schema.rb` from your file system and revision control, add `structure.sql`, and try again.

Problem

Using Rails 3.2.18 and Postgres, I have a few tables for which I have created a view to minimize the data loaded by ActiveRecord. Everything appears to be running correctly when in the application, but when I try to run Rspec I get: ``` ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "properties_view" does not exist ``` (where 'properties_view' is the view on the 'properties' table) What can I do to ensure that Rspec loads views properly from Postgres?

Original source

Related problems