I have an Event model that has one Payoption which can be of different types using STI subclasses like this:
class Event < ActiveRecord::Base
has_one :payoption, dependent: :destroy
accepts_nested_attributes_for :payoption
end
class Payoption < ActiveRecord::Base
belongs_to :event, :polymorphic => true
PAY_OPTION = ["BankPayoption", "CashPayoption"]
end
class BankPayoption < Payoption
belongs_to :event
end
class CashPayoption < Payoption
belongs_to :event
end
I'm creating an Event and a Cash/BankPayoption in the same controller like this, still some hardcoding and missing functionally while developing:
def new
@event = Event.new
@event.build_payoption
end
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
if params[:event][:payoption_attributes][:type] == "BankPayoption"
@payoption = BankPayoption.new
@payoption.phone = "0734176395"
@payoption.event = @event
@payoption.save
@event.payoption = @payoption
end
@event.save
redirect_to @event
end
Both the Event and BankPayoption gets created and BankPayoption gets the corrent Event ID however the Event doesn't get any Payoption ID at all and remains nil. Figured there's issues with the STI/polymorphism but can't figure out how to solve it.
Thanks in advance