【发布时间】:2012-09-22 03:00:24
【问题描述】:
我有 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”的模型具有相同的设置,那么@subscription.plan.nil?评估为假。这令人费解,我花了几个小时试图弄清楚。我是否发现了错误或我做错了什么?也许部分问题是 :foreign_key 是 :plan_id 而不是 :stripe_plan_id?在我在 has_many 中设置 :foreign_key 属性之前,我遇到了另一个错误。
【问题讨论】:
标签: ruby-on-rails ruby activerecord ruby-on-rails-3.2