title: ActiveRecord::StatementInvalid in Controller when using an ActiveRecord with a table name and a postgresql view
body: <p>I have an Event which belongs to Repeat and I am trying to display the repeat title when displaying an event. But I do not want two database calls which this would make </p>
  
  <pre><code>@event.Repeat.title
  </code></pre>
  
  <p>I created a postgresql view using a migration below </p>
  
  <pre><code>class EventWithRepeat &lt; ActiveRecord::Migration[5.1]
    def up
      self.connection.execute %Q( create view event_with_repeat as
                                  Select events.*, repeats.title as RepeatTitle from Events
                                  inner join repeats on events.repeat_id = repeats.id; )
    end
  
    def down
      self.connection.execute %Q( drop view event_with_repeat; )
    end
  
  end
  </code></pre>
  
  <p><strong>The issue is definitely with the migration above. I cannot seem to run just that migration. Executing rake db:migrate does not do anything. rake db:migrate:down with the version results in an error because the relation does not exists so it cannot drop it.</strong> </p>
  
  <p>Creating the SQL view directly in the database does work but my migration does not seem to execute. I am using rails 5. </p>
  
  <p>This is my ActiveRecord class</p>
  
  <pre><code>class EventRepeat &lt; ActiveRecord::Base
    self.table_name = "event_with_repeat"
    self.primary_key = "id"
  
    def readonly?
      true
    end
  end
  </code></pre>
  
  <p>This is the method in my controller class</p>
  
  <pre><code>def show
      @event = EventRepeat.find(params[:id])
  end
  </code></pre>
  
  <p>I ran the migrations and I can the view in psql so the view is present. I did do db:reset but the did not help. </p>
  
  <p>The error message shows that the issue is with this SQL</p>
  
  <pre><code>SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                       pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                       c.collname, col_description(a.attrelid, a.attnum) AS comment
                  FROM pg_attribute a
                  LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                  LEFT JOIN pg_type t ON a.atttypid = t.oid
                  LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation &lt;&gt; t.typcollation
                 WHERE a.attrelid = '"event_with_repeat"'::regclass
                   AND a.attnum &gt; 0 AND NOT a.attisdropped
                 ORDER BY a.attnum
  </code></pre>
  
  <p>and the error message is </p>
  
  <pre><code>ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "event_with_repeat" does not exist
  </code></pre>
  
  <p>but the view event_with_repeat does exist. </p>
  
  <p>I have this in my view</p>
  
  <pre><code>&lt;%= @event.RepeatTitle %&gt;
  </code></pre>
  
  <p>Any ideas on how to fix this error and what was wrong with the code?</p>
  
  <p>Thanks.</p>