Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.2.6 & database views creation through migrations

I'm using rails 3.2.6 and I need to create a database VIEW. As usual I created a migration and I tried to achieve the goal using the execute method.

Unfortunately the migration generates a table, not a view. Why?

Many thanks in advance, Mauro

UPDATE:

I would like to have something as follows:

class CreateMyView < ActiveRecord::Migration
  def self.up
    execute <<-SQL
      CREATE VIEW my_view AS SELECT ...
    SQL
  end
  def self.down
    execute <<-SQL
      DROP VIEW my_view
    SQL
  end
end

Unfortunately this migration creates a table...

UPDATE: the previous code works! I was executing rake db:reset instead of rake db:migrate:reset (my mistake)

like image 767
Mauro Nidola Avatar asked Jul 26 '12 09:07

Mauro Nidola


People also ask

When did rails 7 come out?

March 8, 2022 Rails 7.0.2.3, 6.1. 4.7, 6.0. 4.7, and 5.2. 6.3 have been released!


2 Answers

It looks like you've answered your own question, but I'll make a related suggestion. Try the rails_sql_views gem. That link goes to the original repo on GitHub. It looks like it's not being maintained anymore, though. It would be worth looking at the network graph and trying one of the forks. I'm not positive that any of the forks supports Rails 3.2.6, but I'd suggest looking through them. Christian Eichhorn added support for the mysql2 adapter about three years ago.

like image 116
Michael Stalker Avatar answered Oct 18 '22 20:10

Michael Stalker


I've made a gem called rails_db_views which is compatible with Rails 4, and still maintained.

Regards,

Yacine.

like image 2
Yacine Avatar answered Oct 18 '22 21:10

Yacine