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

On 10 May 2010 17:38, Rafael S. Suguiura <[email protected]> wrote:

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

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to