agusgambina agusgambina - 4 months ago 14
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
models/reports/revenue.rb


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


I changed the extension because I am using Rails 5.0.0

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

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


I am not sure what I am missing

Answer

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'
end

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

Comments