Rails Shopify App更新商店模型

时间:2016-11-05 02:00:18

标签: ruby-on-rails api shopify

我正在尝试确保每个商店模型都更新了我们打开应用时想要的信息。我这样做的方法是检查Shop模型是否为customer_created = true

问题是,当我运行检查和更新Shop模型的方法时,它不起作用。

当我出于某种原因转到index.html.erb时,Shop模型没有更新,但是它返回了正确的Shop模型。如有必要,我怎样才能让它更新商店?

控制器

class HomeController < ShopifyApp::AuthenticatedController
  include ApplicationHelper

  def index
    @shop = Shop.updated(current_shop)
    if @shop.save
      redirect_to current_products_path
    end
  end
...

当访问者安装了应用并访问index.html.erb时,应该确保商店已完全更新。 这是shop.rb

class Shop < ActiveRecord::Base
  include ShopifyApp::Shop
  include ShopifyApp::SessionStorage

  def self.updated(shop)
    @the_shop = shop
    current_shop_api = ShopifyAPI::Shop.current
    unless @the_shop.customer_created
      @the_shop.customer_created = true
      @the_shop.email = current_shop_api.email
      @the_shop.currency = current_shop_api.currency
      @the_shop.domain = current_shop_api.domain
      @the_shop.country_name = current_shop_api.country_name
      @the_shop.money_format = current_shop_api.money_format
      @the_shop.money_with_currency_format = current_shop_api.money_with_currency_format
      @the_shop.name = current_shop_api.name
      @the_shop.shop_owner = current_shop_api.shop_owner
      @the_shop.timezone = current_shop_api.timezone
      @the_shop.save
    end
    return @the_shop
  end
end

最后,这里有正在使用的辅助方法

def current_shop
    return Shop.find_by(shopify_domain: ShopifyAPI::Shop.current.domain)
end

def current_shop_api
    return ShopifyAPI::Shop.current
end

1 个答案:

答案 0 :(得分:0)

您可以节省大量的麻烦,只需进行一次API查询即可获得有关商店的所有信息。为什么要在数据库中保留一个版本?没有意义。您的应用程序没有为该数据增加价值,因此只需在需要时将其生效。