small medium large xlarge

02 Feb 2017, 01:13
Andy Zhao (13 posts)

Hey David, on the fifth beta and I’m a bit confused about setting up the model relationships: On page 184, when setting up customer.rb, I made the following corrections:

class Customer < ApplicationRecord
has_many :customers_shipping_addressES      #addresses as opposed to address

def primary_shipping_address
self.customers_shipping_addressES.find_by(primary: true).addressES  #pluralized again
has_one :customers_billing_address
has_one :billing_address, through: :customers_billing_addressES, source: :address #plural

Now with the original code, I tried calling the primary_shipping_address method for my customer (id 350001, davetron5000), who has a shipping address associated with him, and also has an address created. I get TypeError: superclass must be a Class (Module given).

Not sure how to proceed since Customer.find_by(id: 350001).customers_shipping_addresses.find_by(primary: true) works, but not when I call .address on it. The rest of my models have the same relationships as the ones on page 184 of the book.

02 Feb 2017, 20:38
David Copeland (511 posts)

Could be a typo, but all the code is pulled from the running and tested system. CAn you compare to the code included with the book?

03 Feb 2017, 02:15
Andy Zhao (13 posts)

Hmm I did compare it to the source code included, and it’s the same. File path was: code/materialized views/actual-materialized-view/shine/app/models/customer.rb

Also, I run into the same TypeError when I run rails db:seed, with the error occurring when I define the create_billing_address method.

Here is the code on my Github in case you’re interested.

03 Feb 2017, 13:17
David Copeland (511 posts)

Ah, I see the problem. The “superclass must be a class” is what I missed. It means your active record isn’t inheriting from the right thing, and sure enough:

It’s inheriting from ActiveRecord which is a model. If you change it to ApplicationRecord, everything works.

BTW, your code works against my DB without added all the plurals.

04 Feb 2017, 00:19
Andy Zhao (13 posts)

Ahhhhhh, you know I thought that was my error at one point but I disregarded it. Guess I have some reading to do. Thanks for being thorough and looking through my Github!

10 Feb 2017, 00:12
Andy Zhao (13 posts)

Quick question: on chapter 12, running customer_search_spec.rb and I run into this failure:

  1) Customer Search Search by Email
     Failure/Error: customer_detail = CustomerDetail.find(params[:id])
       PG::ObjectNotInPrerequisiteState: ERROR:  materialized view "customer_details" has not been populated
       HINT:  Use the REFRESH MATERIALIZED VIEW command.
       : SELECT  "customer_details".* FROM "customer_details" WHERE "customer_details"."customer_id" = $1 LIMIT $2
     # ./app/controllers/customers_controller.rb:32:in `show'
     # ------------------
     # --- Caused by: ---
     # PG::ObjectNotInPrerequisiteState:
     #   ERROR:  materialized view "customer_details" has not been populated
     #   HINT:  Use the REFRESH MATERIALIZED VIEW command.
     #   ./app/controllers/customers_controller.rb:32:in `show'

Before this, I had some other errors that were fixed with running the following in terminal:

rails db:environment:set RAILS_ENV=test

rails db:migrate RAILS_ENV=test

The page works just fine though.

EDIT: Never mind, solved it with running the following in terminal:

RAILS_ENV=test rails db:migrate
RAILS_ENV=test rails dbconsole

shine_test=> REFRESH MATERIALIZED VIEW customer_details;
shine_test=> \q

RAILS_ENV=test rails refresh_materialized_views    #testing for errors, didn't get any

rspec spec/customer_search_spec.rb

Also I realized I was running an rspec test and not a jasmine test. Oops.

You must be logged in to comment