【问题标题】:ActiveResource::ResourceNotFound: Failed. Response code = 404. Response message = Not FoundActiveResource::ResourceNotFound:失败。响应代码 = 404。响应消息 = 未找到
【发布时间】:2015-06-09 09:56:32
【问题描述】:

您好,我正在开发一个 Rails 项目,我最近更新了我的项目的开发数据库。我有 sqlite,现在我有 Postgres。

我的产品模型有这个方法:

  def self.update_products!
   ec_products = ElemetalCapital::Product.all
   transaction do
     ec_products.each do |ec_product|
        product = ElemetalCapitalProduct.where(id: ec_product.id).first_or_initialize
        product.spot_id = ec_product.spot
        product.goldtrex_markup ||= 1  # default to a 1% markup
        product.description = ec_product.description
        product.metal = ec_product.metal
        product.weight = ec_product.weight
        product.elemetal_capital_premium = ec_product.premiumBuy
        product.save!
      end
    end
  end

在 Postgres 更新之前,该方法运行正常。 但是,更新后我收到了这个错误,我该如何解决这个问题

[2] pry(main)> Product.update_products!
   (0.5ms)  BEGIN
  ElemetalCapitalProduct Load (0.5ms)  SELECT  "products".* FROM "products" WHERE "products"."type" IN ('ElemetalCapitalProduct') AND "products"."id" = $1  ORDER BY "products"."id" ASC LIMIT 1  [["id", "GKILO-OPM"]]
   (0.4ms)  ROLLBACK
ActiveResource::ResourceNotFound: Failed.  Response code = 404.  Response message = Not Found.
from /Users/enriquesalceda/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activeresource-4.0.0/lib/active_resource/connection.rb:144:in `handle_response'

上面很奇怪的是"products"."id" = $1,它不应该是1美元。

此应用使用供应商 elemetal capital 的 API,该 API 提供有关其产品和价格的信息,然后经过一些计算,我们更新了 shopify 数据库。 仅作记录:

这是我的整个产品模型:

class Product < ActiveRecord::Base
  self.primary_key = :id

  monetize :elemetal_capital_premium_cents, allow_nil: true

  belongs_to :spot

  def to_hash
    instance_variables.each_with_object({}) do |var, hash|
      hash[var.to_s.delete("@")] = instance_variable_get(var)
    end
  end

  def metal_name
    case metal
    when "Ag" then "Silver"
    when "Au" then "Gold"
    when "Pd" then "Palladium"
    when "Pt" then "Platinum"
    end
  end

  def price
    # return 1300 if spot.nil?

    spot_price = spot.ask
    ec_price = spot_price + elemetal_capital_premium
    total_price = ec_price * weight
    gt_price = total_price + (goldtrex_markup / 100 * total_price)
    gt_price.exchange_to(:AUD)
  end

  def shopify_variant_data
    {
      barcode: id,
      price: price.to_s,
      weight: weight,
      weight_unit: "oz",
      grams: weight * 31.1034768
    }
  end

  before_create :shopify_create

  def shopify_create
    data = {
      title: "#{metal_name} - #{description}",
      variants: [
        shopify_variant_data
      ]
    }
    sp = ShopifyAPI::Product.create(data)
    self.shopify_id = sp.id
  end

  before_update :shopify_update

  def shopify_update
    sp = ShopifyAPI::Product.find(shopify_id)
    variant = sp.variants.first
    shopify_variant_data.each do |k, v|
      instance_variable_set("@#{k.to_s}".to_sym, v)
    end
    variant.save!
  end

  def self.update_products!
    ec_products = ElemetalCapital::Product.all
    transaction do
      ec_products.each do |ec_product|
        product = ElemetalCapitalProduct.where(id: ec_product.id).first_or_initialize
        product.spot_id = ec_product.spot
        product.goldtrex_markup ||=  # default to a 1% markup
        product.description = ec_product.description
        product.metal = ec_product.metal
        product.weight = ec_product.weight
        product.elemetal_capital_premium = ec_product.premiumBuy
        product.save!
      end
    end
  end
end

这是架构:

ActiveRecord::Schema.define(version: 20150609085027) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "deliveries", force: :cascade do |t|
    t.string   "receiver"
    t.datetime "delivery_day"
    t.string   "tracking_number"
    t.text     "delivery_notes"
    t.datetime "created_at",      null: false
    t.datetime "updated_at",      null: false
    t.integer  "ticket_id"
  end

  create_table "elemetal_capital_trades", force: :cascade do |t|
    t.string  "location"
    t.string  "currency"
    t.string  "side"
    t.string  "elemetal_capital_product_id"
    t.integer "quantity"
    t.string  "elemetal_capital_trade_id"
    t.float   "price_per_unit"
    t.float   "weight"
    t.float   "price_per_weight"
    t.float   "price_total"
    t.string  "time_stamp_origin"
    t.string  "metal"
    t.float   "spot"
    t.integer "line_item_id"
  end

  add_index "elemetal_capital_trades", ["line_item_id"], name: "index_elemetal_capital_trades_on_line_item_id", using: :btree

  create_table "employees", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "items", force: :cascade do |t|
    t.integer  "quantity"
    t.string   "item_description"
    t.float    "unit_price"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "ticket_id"
  end

  create_table "line_items", force: :cascade do |t|
    t.integer  "quantity"
    t.integer  "shopify_line_item_id"
    t.integer  "order_id"
    t.datetime "created_at",                     null: false
    t.datetime "updated_at",                     null: false
    t.integer  "shopify_product_id",   limit: 8
  end

  add_index "line_items", ["order_id"], name: "index_line_items_on_order_id", using: :btree

  create_table "orders", force: :cascade do |t|
    t.integer  "order_number"
    t.integer  "shopify_order_id"
    t.integer  "total"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
  end

  create_table "payments", force: :cascade do |t|
    t.datetime "value_date"
    t.integer  "reference_number"
    t.float    "contract_rate",    default: 0.0
    t.string   "trade_notes"
    t.datetime "created_at",                     null: false
    t.datetime "updated_at",                     null: false
    t.integer  "ticket_id"
    t.float    "usd_payment",      default: 0.0
  end

  create_table "products", id: false, force: :cascade do |t|
    t.string   "id",                                       null: false
    t.string   "type",                                     null: false
    t.text     "description",                              null: false
    t.decimal  "weight",                                   null: false
    t.string   "metal",                                    null: false
    t.string   "spot_id",                                  null: false
    t.integer  "elemetal_capital_premium_cents"
    t.decimal  "goldtrex_markup",                          null: false
    t.datetime "created_at",                               null: false
    t.datetime "updated_at",                               null: false
    t.integer  "shopify_id",                     limit: 8, null: false
  end

  add_index "products", ["shopify_id"], name: "index_products_on_shopify_id", unique: true, using: :btree
  add_index "products", ["spot_id"], name: "index_products_on_spot_id", using: :btree

  create_table "spots", id: false, force: :cascade do |t|
    t.string  "id",        null: false
    t.integer "bid_cents", null: false
    t.integer "ask_cents", null: false
  end

  create_table "tickets", force: :cascade do |t|
    t.integer  "goldtrex_employee"
    t.string   "ticket_number"
    t.datetime "elemetal_capital_order_date"
    t.string   "trader"
    t.datetime "created_at",                  null: false
    t.datetime "updated_at",                  null: false
    t.boolean  "au"
    t.float    "au_spot_price"
    t.boolean  "ag"
    t.float    "ag_spot_price"
    t.boolean  "deposit"
    t.float    "deposit_amount"
  end

end

【问题讨论】:

  • 它说找不到活动资源。你只在api上工作吗? Bcoz 当您的两个应用程序相互通信时,就会出现此类错误。一般一个是后端,一个是前端。
  • Elemetal Capital API 提供价格和现货价格,我们将其存储在我们的数据库中,然后我在 Shopify 中更新产品。
  • 那么您在更新产品时遇到了问题?
  • 是的,我正在运行 binding.pry 并且我正在检查每个方法,并且要插入的数据是这样的($2、$3、$4、$5、$6、$7.. .
  • 当你更新你的产品到shopify时,你不需要认证?您如何进行身份验证?您不必传递令牌来更新产品?

标签: ruby-on-rails postgresql sqlite


【解决方案1】:

Rails 使用 ORM,它将所有使用 DB 的逻辑隐藏在好的方法中。基本上这意味着如果您更改数据库 - 什么都不会发生,应用程序将继续按预期工作(应该提到,此语句在任何情况下都不适用,因为数据库不同,但在这种情况下不适用)。如果你得到 404 - 这意味着数据库中缺少项目,这没有错。

当您说您将数据库从 sqlite 更改为 Postgres 时,您是否迁移了数据?尝试从控制台运行ElemetalCapitalProduct.count 以确保它有任何东西。如果是,请将您在 sqlite 中的数据与您在 Postgres 中收到的数据进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多