# Rake replaces my view with a table

**URL:** <https://www.ruby-forum.com/t/rake-replaces-my-view-with-a-table/90243>\
**Category:** Rails\
**Created:** [March 27, 2007, 1:14am UTC](https://www.ruby-forum.com/t/rake-replaces-my-view-with-a-table/90243 "2007-03-27T01:14:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [March 27, 2007, 1:14am UTC](https://www.ruby-forum.com/t/rake-replaces-my-view-with-a-table/90243/1 "2007-03-27T01:14:21Z")

</div>

Hi rails-folks,

I have a non-UPDATE-able MySQL view which contains aggregate data for  
another table (AVG, STDDEV for 3 columns). This view is used as the  
basis for a model, “Accountingstat”. The view is created in a  
migration which simply "execute"s the sql required to create it.

If I run:

RAILS\_ENV=test rake db:migrate

The view is created as a view, which I can verify by doing:

mysql\> SHOW CREATE VIEW resultsviewer\_test.accountingstats;  
(view data omitted)

If I run

rake

and then do the same command:

mysql\> SHOW CREATE VIEW resultsviewer\_test.accountingstats;  
ERROR 1347 (HY000): ‘resultsviewer\_test.accountingstats’ is not VIEW

mysql\> SHOW CREATE TABLE resultsviewer\_test.accountingstats;  
(table with exactly the same structure as the view, but not a view)

This table is empty, so my tests fail.

One very inelegant way to address this is to load data into the  
_table_ with a fixture. However, since on first creation this will  
presumably be a view, which is not updateable, I think the test will  
fail if I do try to insert data with a fixture. Additionally, I’m  
hoping to be able to use the model unit test to test the db logic (as  
I’m at least 80% novice).

Any thoughts on how I can get my test to run with a view?

Matthew

## –

Matthew Hall  
removed\_email\_address@domain.invalid Try not to read it as “hamtheallwet”.

---

<div class="post-metadata">

**Author:** ![matthew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@matthew](https://www.ruby-forum.com/u/matthew)\
**Post date:** [March 27, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/rake-replaces-my-view-with-a-table/90243/2 "2007-03-27T22:30:03Z")

</div>

Hello again rails people,

After some investigation I’ve had to stop working on solving this  
issue and instead fall back on using a fixture to load data into the  
“table” that rake so nicely replaces my view with.

For the benefit of the googlers, here is some of what I’ve learned in  
my investigation:

rake drops all the tables in the test database and recreates them  
using schema.db. schema.db is re-generated by rake prior to this  
action, so modifying it isn’t useful.

The “correct” way to handle this would be to extend activerecord to be  
able to create and manage views and not just tables. See  
[http://activewarehouse.rubyforge.org/rails\_sql\_views/rdoc/](http://activewarehouse.rubyforge.org/rails_sql_views/rdoc/) for  
documentation on rails\_sql\_views, which seems to do this for MySQL and  
PostgreSQL. Once I have time I’ll evaluate the gem and see if it will  
fit my needs.

A work around, for those more knowledgeable than I, would be to  
override some of the methods which rake calls to do its database work.  
Executing “rake test:units --trace” will show you to some degree what  
is going on with the database.

Matthew

ps. I’m not sure what keywords to include to make this easy to find -  
“activerecord” “sql” and “view” aren’t that useful, but perhaps  
together: “activerecord sql view”. Having a hard-to-search for  
problem makes it much more difficult to find a solution.

On 3/26/07, Matthew [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> presumably be a view, which is not updateable, I think the test will  
> Matthew Hall  
> removed\_email\_address@domain.invalid Try not to read it as “hamtheallwet”.

## –

Matthew Hall  
removed\_email\_address@domain.invalid Try not to read it as “hamtheallwet”.
