Ruby on rails 在调用操作后将列从NULL更新

Ruby on rails 在调用操作后将列从NULL更新,ruby-on-rails,Ruby On Rails,“我的应用”上已设置条带,用户可以取消或升级其订阅。它与条纹完美沟通。我需要帮助弄清楚如何使更改与数据库通信 如果用户取消订阅,则应在订阅表中的已取消列下标记该订阅。如果在数据库中显示用户的订阅已被取消,用户将被限制访问该网站 我不确定如何将其添加到我已设置的取消订阅操作中 在此方面的帮助将不胜感激 订阅控制器: def new plan = Plan.find(params[:plan_id]) @subscription = plan.subscriptions.buil

“我的应用”上已设置条带,用户可以取消或升级其订阅。它与条纹完美沟通。我需要帮助弄清楚如何使更改与数据库通信

如果用户取消订阅,则应在订阅表中的
已取消
列下标记该订阅。如果在数据库中显示用户的订阅已被取消,用户将被限制访问该网站

我不确定如何将其添加到我已设置的取消订阅操作中

在此方面的帮助将不胜感激

订阅控制器:

  def new
    plan = Plan.find(params[:plan_id])
    @subscription = plan.subscriptions.build
    if params[:PayerID]
      @subscription.paypal_customer_token = params[:PayerID]
      @subscription.paypal_payment_token = params[:token]
      @subscription.email = @subscription.paypal.checkout_details.email
    end
  end

  def create
    @subscription = Subscription.new(params[:subscription])
    if @subscription.save_with_payment
      redirect_to @subscription, :notice => "Thank you for subscribing!"
    else
      render :new
    end
  end

  def show
    @subscription = Subscription.find(params[:id])
  end

    def updatesubscription
      @user = current_user
      @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
      @customer.update_subscription(:plan => "1", :prorate => true)
     current_user.save!
      flash.alert = 'Your subscription has been updated!'
      redirect_to root_url
     end

     def cancelsubscription
       @user = current_user
         @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
         @customer.cancel_subscription()
         current_user.save!
         flash.alert = 'Your subscription has been cancelled successfully!'
         redirect_to root_url
       end

       def showcard
         @user = current_user
         Stripe::Customer.retrieve(@user.subscription.stripe_customer_token).cards.all()
       end

       def changecard
           @user = current_user       
           @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)

             card = @customer.cards.create({
               :card => @user.subscription.stripe_customer_token
             })

             @customer.default_card = card
             @customer.save
           end
end
订阅模式:

  belongs_to :plan
  belongs_to :subscription
  belongs_to :user

  validates_presence_of :plan_id
  validates_presence_of :email

  attr_accessor :stripe_card_token, :paypal_payment_token

  def save_with_payment
    if valid?
      if paypal_payment_token.present?
        save_with_paypal_payment
      else
        save_with_stripe_payment
      end
    end
  end

  def paypal
    PaypalPayment.new(self)
  end

  def save_with_paypal_payment
    response = paypal.make_recurring
    self.paypal_recurring_profile_token = response.profile_id
    save!
  end

  def save_with_stripe_payment
    customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
    self.stripe_customer_token = customer.id
    save!
  rescue Stripe::InvalidRequestError => e
    logger.error "Stripe error while creating customer: #{e.message}"
    errors.add :base, "There was a problem with your credit card."
    false
  end

  def payment_provided?
    stripe_card_token.present? || paypal_payment_token.present?
  end

  def suspend_paypal
    paypal.suspend
    save
  end


  def reactivate_paypal
    paypal.reactivate
    save
  end
end

您不能通过活动记录查询更新订阅表吗?像这样:

updateQuery = ActiveRecord::Base.connection.execute("UPDATE Subscription SET cancelled = 'Cancelled' WHERE user_id = #{@user.id}")
这将更新订阅表并将目标字段值更改为“已取消”。不清楚这是否是布尔值。如果是,那么值应该是“TRUE”,而不是我在其中输入的“Cancelled”字符串

如果订阅成功保存,则执行查询。但您必须确保将查询放在两个条件之间,以便它仅在用户取消订阅时执行,而不是每次更新订阅时执行

respond_to do |format|
  if @subscription.save
    if (something to tell the controller you are cancelling the subscription)
       EXECUTE QUERY HERE
    end
  end
end

最简单的方法是添加额外的行来更新所需的列

def cancelsubscription
  @user = current_user
  @customer = Stripe::Customer.retrieve(@user.subscription.stripe_customer_token)
  @customer.cancel_subscription()
  current_user.subscription.update_attributes(:cancelled => 1)
  current_user.save!
  flash.alert = 'Your subscription has been cancelled successfully!'
  redirect_to root_url
end
然而,理想情况下,所有这些都应该发生在一个模型中。从控制器上,您应该只是告诉模型取消订阅

因此,这种方法可能会变成:

#subscriptions_controller.rb
def cancelsubscription
  current_user.subscription.cancel_stripe_subscription
  flash.alert = 'Your subscription has been cancelled successfully!'
  redirect_to root_url
end

#subscription.rb model
def cancel_stripe_subscription
  customer = Stripe::Customer.retrieve(self.stripe_customer_token)
  customer.cancel_subscription()
  self.cancelled = 1
  self.save!
end

由于我不熟悉以这种方式使用ActiveRecord,因此我不得不对此进行更多的研究。将发布更新。使用活动记录查询和回调之间有什么区别?嗯,我不太熟悉回调,但我想说这两种方法之间的主要区别是回调需要采取一个操作来触发事件(before.save/after.save),而查询是处理数据库的更直接的方法。我相信两者都有各自的优点和缺点。好吧,这就是我在阅读时的想法。我需要在保存订阅后更新数据库。我试图理解您的代码,但它似乎不是只有在处理订阅后才会更新。虽然我没有任何执行查询的经验,因此我可能会出错。您可以在评估订阅是否保存后执行此查询。检查我修改过的答案。