I have this class Churned
module AccountActivities
class Churned < AccountActivity
belongs_to :subscription, class_name: 'NewSubscription'
end
end
now, I have another subscription table called SubscriptionA.
what I am trying to achieve is
@subscription_activity = AccountActivities::Chunrned.create!(
subscription: subscription(in this case this could be NewSubscription or SubscriptionA class instance),
)
I want to be able to pass two different class instances (NewSubscription or SubscriptionA) to :subscription when I am creating activity. I do not want to change currect database schema, so polymorphic assosciation as :subscriptable something that I do not want to do.
I tried something like this but that does not work
class Churned < AccountActivity
belongs_to :plan
belongs_to :subscription, class_name: :determine_subscription_class
private
def determine_subscription_class
if some_condition about :plan
'NewSubscription'
else
'SubscriptionA'
end
end
end
I know there is already a comment saying the polymorphic association is your choice. But let there be an answer.
In your migration you have to use
Or manually add 2 fields:
subscription_idandsubscription_typeand indexes.You won't be able to rely on foreign key constrains if using any, that's beyond standard behavior of many DB's (Postgres, e.g.), but that should not be a big deal.