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:
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:
Coupon
type from CouponApplication
to
ApprovedCoupon
ApprovedCoupon
model so I decided to create a new ApprovedCoupon
record instead of just changing the type.Is there any better approach?
You could add an approve
method to your CouponApplication
model like this:
class CouponApplication < Coupon
...
def approve
data = attributes.except('id', 'created_at', 'updated_at')
ApprovedCoupon.create(data.merge(type: 'Advertisement'))
end
end
Now your code can be simplified to this:
@coupon_application = CouponApplication.find(params[:id])
@approved_coupon = @coupon_application.approve