I am wondering how I could create a custom data type to use within the rake migration file. Example: if you would be creating a model, inside the migration file you can add columns. It could look like this:
def self.up
create_table :products do |t|
t.column :name, :string
t.timestamps
end
end
I would like to know how to create something like this:
t.column :name, :my_custom_data_type
The reason for this to create for example a “currency” type, which is nothing more than a decimal with a precision of 8 and a scale of 2. Since I use only MySQL, the solution for this database is sufficient enough.
Thank you for your feedback and comments!
What you’re looking to do is define a new column creation method that provides the options to create your custom type. Which is essentially done by adding a method that behaves like
t.integer ...in migrations. The trick is figuring out where to add that code.Some where in your initializers directory place this snippet of code:
Now you can use the currency method do define a currency column any time you need it.
Example:
To add a currency column to an existing table:
Caveat: I haven’t time to test it, so there’s no guarantees. If it doesn’t work, it should at least put you on the right track.