Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 710981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:41:02+00:00 2026-05-14T04:41:02+00:00

I am wondering how I could create a custom data type to use within

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:41:02+00:00Added an answer on May 14, 2026 at 4:41 am

    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:

    module ActiveRecord::ConnectionAdapters
      class TableDefinition
        def currency (*args)
          options = args.extract_options!
          column_names = args
          options[:precision] ||= 8
          options[:scale] ||= 2
          column_names.each { |name| column(name, 'decimal', options) }
        end                                                                     
      end
    end
    

    Now you can use the currency method do define a currency column any time you need it.

    Example:

    def self.up
      create_table :products do |t|
        t.currency :cost
        t.timestamps
      end
    end
    

    To add a currency column to an existing table:

    def self.up
      change_table :products do |t|
        t.currency :sell_price
      end
    end   
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer select into outfile can only create the file on the… May 14, 2026 at 7:23 pm
  • Editorial Team
    Editorial Team added an answer I was unaware of the bit.ly API, here is the… May 14, 2026 at 7:23 pm
  • Editorial Team
    Editorial Team added an answer You'll want to use a push based parser that emits… May 14, 2026 at 7:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.