由于不存在字段

时间:2018-05-18 20:57:33

标签: ruby-on-rails ruby postgresql

无法解决这个问题。我在Ruby应用程序上运行Postgres,我的架构如下所示:

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

我确实从shopify_domain表中移除了Amazons。但是我运行了这个迁移,你可以在我的架构中看到它已经消失了。

但是我尝试create新记录,我收到此错误消息: NoMethodError (undefined method shopify_domain for #<Amazon:0x000000041fb9c8>

我创建了新的Amazon记录,通过这样的方式确定商店:

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

current_shop是一种从会话中获取当前商店的方法。它工作正常。

我哪里误入歧途?

编辑:我去检查PG确定,而且他们也不是。以下是PG的内容:

 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[]         | 

编辑:这是Amazon模型

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

然后这是整个错误消息:

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'

然后是从底部向上开始的与错误消息匹配的代码行: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?}

然后app/controllers/concerns/amazon_creds_concern.rb:23:in

save_amazon_creds(marketplace, seller_id, auth_token, countries)

最后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)

1 个答案:

答案 0 :(得分:1)

include ShopifyApp::SessionStorage

添加

validates :shopify_domain, presence: true, uniqueness: true

到你的亚马逊班。

您需要该类中的shopify_domain(或使用转发),或者您需要从Amazon类中删除包含。

顺便提一句shopify_token

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