invitation.rb is as:
class Invitation < ApplicationRecord .
belongs_to :sender, class_name: 'User'
belongs_to :subscription
end
subscription.rb is as:
class Subscription < ApplicationRecord
has_many :payments, dependent: :destroy
has_one :invitation,
belongs_to :plan
belongs_to :user
end
and the migration file for adding column subscription_id in invitations is as:
class AddSubscriptionIdToInvitations < ActiveRecord::Migration[5.1]
def change
add_reference :invitations, :subscription, foreign_key: true
end
end
Although I haven't specified validation rule in Invitation model for subscription_id presence. But I get error 'Subscription not present', when I do below code:
@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 version = 5.1.4 and ruby version = 2.4.3. Thank you.