# Creating a database view in a Rails way without ActiveCouch?

**URL:** <https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355>\
**Category:** Rails\
**Created:** [May 10, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355 "2010-05-10T19:52:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafael\_SSSuguiura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@Rafael\_SSSuguiura](https://www.ruby-forum.com/u/Rafael_SSSuguiura)\
**Post date:** [May 10, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355/1 "2010-05-10T19:52:55Z")

</div>

Is it possible to create a database view in a Rails way without  
ActiveCouch?

---

<div class="post-metadata">

**Author:** ![Rafael\_SSSuguiura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@Rafael\_SSSuguiura](https://www.ruby-forum.com/u/Rafael_SSSuguiura)\
**Post date:** [May 10, 2010, 8:25pm UTC](https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355/2 "2010-05-10T20:25:42Z")

</div>

Hey guys,

good news!

I’ve just found out a way to do the job (in a not so clean way) by  
creating  
a new migration and executing a raw sql.

For instance, I’m going to use a _Session_ which has a _start_ and  
_end_datetime as attributes and  
_duration_ as a derived attribute.

So, I created an empty migration:

script/generate migration create\_sessions\_view

and filled it as follows:

class CreateSessionsView \< ActiveRecord::Migration  
def self.up  
create\_view\_sql = “CREATE VIEW sessions\_view AS SELECT \*,  
strftime(’%s’,end) - strftime(’%s’,start) AS duration FROM sessions”  
execute(create\_view\_sql)  
end

def self.down  
drop\_view\_sql = “DROP VIEW sessions\_view”  
execute(drop\_view\_sql)  
end  
end

And to finish it up:

rake db:migrate

I hope it helps those who are willing to create a view in the future.

(I think it would be better if ActiveRecord::Migration had a create\_view  
method, but as it doesn’t have for now, I think this is the simplest  
work  
around =)

Cheers,

Rafael

---

<div class="post-metadata">

**Author:** ![Rafael\_SSSuguiura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@Rafael\_SSSuguiura](https://www.ruby-forum.com/u/Rafael_SSSuguiura)\
**Post date:** [May 10, 2010, 8:56pm UTC](https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355/3 "2010-05-10T20:56:42Z")

</div>

Rafael S. Suguiura wrote:

> Is it possible to create a database view in a Rails way without  
> ActiveCouch?

rails\_sql\_views plugin, I believe. Or did you mean a CouchDB view?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Rafael\_SSSuguiura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df705f/32.png) [@Rafael\_SSSuguiura](https://www.ruby-forum.com/u/Rafael_SSSuguiura)\
**Post date:** [May 11, 2010, 1:12am UTC](https://www.ruby-forum.com/t/creating-a-database-view-in-a-rails-way-without-activecouch/188355/4 "2010-05-11T01:12:33Z")

</div>

On 10 May 2010 15:56, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Rafael S. Suguiura wrote:
> 
> > Is it possible to create a database view in a Rails way without  
> > ActiveCouch?
> 
> rails\_sql\_views plugin, I believe. Or did you mean a CouchDB view?

It’s not the CouchDB view (it would be too overpower to my app…), but  
the  
plugin seems to do the job. I’ll try it later! 🙂

Thanks!
