ToddT
ToddT

Reputation: 3258

Create not saving record due to non-existent field

Can't figure this out. I'm running Postgres on a Ruby app and my schema looks like this:

ActiveRecord::Schema.define(version: 20180518200146) do
  create_table "amazons", force: :cascade do |t|
    t.text "auth_token"
    t.text "marketplace"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "shop_id"
    t.boolean "three_speed"
    t.text "seller_id"
    t.string "shipping_countries", array: true
  end

  create_table "shops", force: :cascade do |t|
    t.string "shopify_domain", null: false
    t.string "shopify_token", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.boolean "paid", default: false
    t.boolean "setup", default: false
    t.string "carrier_id"
    t.index ["shopify_domain"], name: "index_shops_on_shopify_domain", unique: true
  end

  add_foreign_key "amazons", "shops"
end

I did remove shopify_domain from the Amazons table. But I ran that migration and as you can see in my schema its gone.

However I try and create a new record and I get this error message: NoMethodError (undefined method shopify_domain for #<Amazon:0x000000041fb9c8>

I'm creating the new Amazon record, by scoping it to the shop like this:

current_shop.amazons.create(
  marketplace: marketplace,
  seller_id: seller_id,
  auth_token: auth_token,
  shipping_countries: countries,
  shop_id: current_shop.id)

With current_shop being a method that gets the current shop from the session. It works fine.

Where did I go astray?

EDIT: I went and checked PG to be sure, and the field isn't their either. Here is what PG has:

 id                 | bigint                      | not null default nextval('amazons_id_seq'::regclass)
 auth_token         | text                        | 
 marketplace        | text                        | 
 created_at         | timestamp without time zone | not null
 updated_at         | timestamp without time zone | not null
 shop_id            | integer                     | 
 three_speed        | boolean                     | 
 seller_id          | text                        | 
 shipping_countries | character varying[]         | 

EDIT: Here is the Amazon model

class Amazon < ActiveRecord::Base
  include ShopifyApp::SessionStorage
  belongs_to :shop
end

And then this is the entire error message:

NoMethodError (undefined method `shopify_domain' for #
<Amazon:0x000000041fb9c8>
Did you mean?  shop_id_change):

app/controllers/concerns/amazon_creds_concern.rb:55:in `save_amazon_creds'
app/controllers/concerns/amazon_creds_concern.rb:23:in `create_amazon_client'
app/controllers/amazon_creds_controller.rb:9:in `amazon_credentials_check'

And then the lines of code that match the error message, starting from the bottom up: app/controllers/amazon_creds_controller.rb:9:in amazon_credentials_check

render json: {amazon_creds_status: create_amazon_client(marketplace, seller_id, auth_token, countries), countries: countries.blank?}

Then app/controllers/concerns/amazon_creds_concern.rb:23:in

save_amazon_creds(marketplace, seller_id, auth_token, countries)

Finally app/controllers/concerns/amazon_creds_concern.rb:55:in

current_shop.amazons.create(
  marketplace: marketplace,
  seller_id: seller_id,
  auth_token: auth_token,
  shipping_countries: countries,
  shop_id: current_shop.id)

Upvotes: 1

Views: 51

Answers (1)

Shadow Radiance
Shadow Radiance

Reputation: 1369

include ShopifyApp::SessionStorage

adds

validates :shopify_domain, presence: true, uniqueness: true

to your Amazon class.

You either need the shopify_domain in that class (or use a forward) or you need to remove the include from the Amazon class.

Same for shopify_token by the way.

https://github.com/Shopify/shopify_app/blob/master/lib/shopify_app/session/session_storage.rb

Upvotes: 1

Related Questions