After installing STI, you will have 2 different forms for each child class. This means that you will have fields in the form for Event and Discount , respectively, so when you save the Discount record, the second image_file field will have a nil value by default (if you didnβt change it to something else).
Now all the attributes of your Promo class are actually methods that you can override, so if in Discount you do:
def additional_image_filename
it will always return nil as a result no matter what was stored there. An example in the console based on my application where I override the name attribute (not the one I need, it just shows up):
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 the database and it matters, but returns nil because it was overridden.
You can also add checks for individual classes or callbacks that will clear attributes that you do not need, for example, the so-called additional_image_filename , but I donβt understand why you need it. for each form you will have separate controllers and actions that I assume, and therefore there will be different permitted_params that allow you to choose which fields should only be saved.
In short:
- Do not include
additional_image_filename in the discount form - Do not include
additional_image_filename in peritted_params in controller action for discount - Set the value as nil in db or override the method in the
Discount class as described above.
source share