agusgambina agusgambina - 1 year ago 109
Ruby Question

Rails how to pass parse data from sql view to model?

I followed the excellent post from Frank Rietta about "Adding a Rake Task for SQL Views to a Rails Project". I like his point of view about database views in rails and his dry approach.

I am able to do

rake db:views
and my view is created but I am not able to get the information in the model, this is my

class Report::Revenue < ApplicationRecord
self.table_name = 'report_revenues'

I changed the extension because I am using Rails 5.0.0

If I execute the
rails console --sandbox
and there I execute
I get the following

2.3.1 :004 > Report::Revenue
NameError: uninitialized constant Report

I am not sure what I am missing

Answer Source

I think Rails expects the module name and the folder name to match. Note that you mixed singular and plural.

That said you have to change your model to:

class Reports::Revenue < ApplicationRecord
  self.table_name = 'report_revenues'

Or move your model into a folder named models/report/revenue.rb.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download