I have a model called Coupons
Then I have two child models CouponApplications
and ApprovedCoupons
.
The last two inherit from Coupons
via an STI architecture.
Now I want to realise the following:
- A user sees CouponApplications
- He clicks on an Approve button which makes the
CouponApplications
ApprovedCoupons
I realise that I could simply update the type
column of the Coupons
record to change types. However, there are several Concerns, hooks etc in the ApprovedCoupons
model which are happening after creation so this is not so easy. In fact, I want to create a complete new record to trigger those Concerns, hooks etc.
So I wrote this which I consider really bad:
@coupon_application = CouponApplication.find(params[:id])
@approved_coupon = ApprovedCoupon.new
# copy/paste attributes except the ID as this would be considered a duplication
@approved_coupon.attributes = @coupon_application.attributes.except("id")
# set the new type
@approved_coupon.update_attributes(type: "Advertisement")
@approved_coupon.save
I hope you understand what I want to achieve. It works this way but I doubt this is clean code.
To summarize:
- I want to change the
Coupon
type fromCouponApplication
toApprovedCoupon
- I still want to trigger the Concerns, hooks etc. in my
ApprovedCoupon
model so I decided to create a newApprovedCoupon
record instead of just changing the type.
Is there any better approach?