邀请.rb 为:
class Invitation < ApplicationRecord .
belongs_to :sender, class_name: 'User'
belongs_to :subscription
end
subscription.rb 为:
class Subscription < ApplicationRecord
has_many :payments, dependent: :destroy
has_one :invitation,
belongs_to :plan
belongs_to :user
end
在邀请中添加列 subscription_id 的迁移文件如下:
class AddSubscriptionIdToInvitations < ActiveRecord::Migration[5.1]
def change
add_reference :invitations, :subscription, foreign_key: true
end
end
虽然我没有在邀请模型中为订阅 ID 存在指定验证规则。但是当我执行以下代码时,出现错误“订阅不存在”:
@invitation = Invitation.create_with(
message: invitation_params.delete(:message),
first_name: invitation_params.delete(:first_name),
last_name: invitation_params.delete(:last_name),
).find_or_create_by(
receiver: receiver,
sender: current_user
)
Rails 版本 = 5.1.4 和 ruby 版本 = 2.4.3。谢谢你。