0

次のように2つのモデルをセットアップしています。

class StripePlan < ActiveRecord::Base
  attr_accessible :location, :name, :price, :plan_type
  has_many :stripe_subscriptions, :foreign_key=>:plan_id
end

class StripeSubscription < ActiveRecord::Base
  attr_accessible :email, :plan_id, :stripe_customer_token, :teacher_id
  belongs_to :stripe_plan
  belongs_to :teacher
end

class Teacher < ActiveRecord::Base
   has_one :stripe_subscription, dependent: :destroy
end

Railsコンソールで次のようなことをします:

 @stripe_plan = StripePlan.find(params[:plan_id])
 @stripe_subscription = @stripe_plan.stripe_subscriptions.build(:teacher_id=>params[:teacher_id],:plan_id=>params[:plan_id],:email=>params[:email])

ここで、params={:teacher_id=>1, :plan_id=>1, :email="example@example.com"}. ここでは、id が 1 のプランと教師が存在すると想定できます。

今、@stripe_subscription.stripe_plan.nil? true と評価されます。 しかし、そうすべきではありません。「サブスクリプション」と呼ばれるモデルと「プラン」と呼ばれるモデルが同じセットアップであった場合、@subscription.plan.nil? false と評価されます。これは不可解で、私はそれを理解しようと数時間費やしました. バグを見つけましたか、それとも何が間違っていますか? おそらく問題の一部は、:foreign_key が :stripe_plan_id ではなく :plan_id であることですか? has_many で :foreign_key 属性を設定するまで、別のバグが発生していました。

4

2 に答える 2

1

@stripe_plan.saveアイテムを DB に保存する必要があります。それはまだ記憶にあります。オブジェクトに保存されているため、実行した@stripe_plan.stripe_subscriptions場合でも表示されます@stripe_plan。保存@string_planするまでDBにはありません。

あなたもできる
@stripe_subscription = @stripe_plan.stripe_subscriptions.create(:teacher_id=>params[:teacher_id],:plan_id=>params[:plan_id],:email=>params[:email])

アイテムを作成してDBに保存します

于 2012-11-27T19:25:03.273 に答える
0

を使用するので、:plan_id=>params[:plan_id]inを使用する必要はありません。buildstripe_planid

もう 1 つすべきことは、cosole を確認することです。ホワイトリストに登録していない属性を見落としている可能性があります。

Rails 3 -- ビルドがデータベースに保存されない (Railscast 196) を確認してください。同様の問題に遭遇したようです。

于 2012-09-22T03:26:01.183 に答える