Creating a database view in a Rails way without ActiveCouch?

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

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

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,

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! :slight_smile:

Thanks!