Having STI setup means you'll have 2 different forms for each child Class. That means you'll have fields available in the form accordingly for Event
and for Discount
, so when you'll save a Discount
record the second image_file field will have a nil
value by default (if you didn't changed it to something else).
Now, all attributes of your Promo
class are actually methods that you can override, so if in Discount
you'll do:
def additional_image_filename #name of the attribute
nil
end
it will always return nil as result no matter what it was saved there. An example in console based on my app where I override the name
attribute (not that I need, just showing):
2.1.5 :028 > Recipe.last
Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" ORDER BY "recipes"."id" DESC LIMIT 1
=> #<Recipe id: 4, name: "Potatoes Au Gratin", instructions: nil, created_at: "2014-12-04 12:54:26", updated_at: "2014-12-04 12:54:26">
2.1.5 :029 > Recipe.last.name
Recipe Load (0.4ms) SELECT "recipes".* FROM "recipes" ORDER BY "recipes"."id" DESC LIMIT 1
=> nil
as you can see there is a name in database and it has a value, but it returns nil
cause it was overridden.
Also you could add validations for separate Classes or callbacks that will make sure to clean up the attributes you don't need like the so called additional_image_filename
, but I don't see why you'd need this.. cause for each form you'll have separate controllers and actions I guess and therefore there will be different permitted_params
that will let you choose what fields should allowed to be saved only.
In short:
- Don't include
additional_image_filename
in form for Discount
- Don't include
additional_image_filename
in peritted_params
in controller action for Discount
- Set the value as nil in db or override the method in
Discount
class as described above.
Discount
class to know about its sibling's (Event
)image_filename
attribute – Bicknellimage_filename
, the attribute in reality is a property of Promo, other thing is where you use it – Covenantee