clear
clear copied to clipboard
Custom polymorphic through values
Currently when using a polymorphic model, the type
column is generated to be like Animal::Cow
or Animal::Chicken
, or whatever the full class name of your model is.
This conflicts when wanting to use a custom discriminator. I.e. specify a column type : String
and use that column/value for determining which model should be used.
The use case for this is data which would already have a discriminator to use, that would not be dependent on the model names being used.
I don't understand clearly the problem here, can you give an example of the issue encountered?
If the problem is the usage of the column type
, you may change the value to something else using polymorphic ... through: ...
optional construction:
polymorphic Cow, Chicken, through: "my_custom_column"
If the question is to setup a map between model and column name, like this:
polymorphic({"cow" => Animal::Cow, "chick" => Animal::Chicken})
Then I don't think I'm going to implement it in short term (since I want polymorphism to be stable at first :+1:)
For example:
class Contract
column contract_id : Int32, primary: true
column type : String
polymorphic({"courier" => Contract::Courier, "item_exchange" => Contract::ItemExchange, "auction" => Contract::Auction})
end
By default it would still use the type
column, but could use the through
to define another column if you wanted, but which subclass to use would be mapped to a custom value, like your cow
or chicken
vs Animal::Cow
.
In my use case the data ill be saving, already has a type
property which can be used to determine the type of contract it is. So being able to manually set this up makes more sense.
Yea no worries :) Just made this for tracking, as i think it would be a nice feature, not expecting it anytime soon.