Rails: creating a custom data type, to use with generator classes and a bunch of questions related t
Posted
by Shyam
on Stack Overflow
See other posts from Stack Overflow
or by Shyam
Published on 2010-04-20T15:20:42Z
Indexed on
2010/04/20
15:23 UTC
Read the original article
Hit count: 271
Hi,
After being productive with Rails for some weeks, I learned some tricks and got some experience with the framework. About 10 days ago, I figured out it is possible to build a custom data type for migrations by adding some code in the Table definition.
Also, after learning a bit about floating points (and how evil they are) vs integers, the money gem and other possible solutions, I decided I didn't WANT to use the money gem, but instead try to learn more about programming and finding a solution myself.
Some suggestions said that I should be using integers, one for the whole numbers and one for the cents. When playing in script/console, I discovered how easy it is to work with calculations and arrays. But, I am talking to much (and the reason I am, is to give some sufficient background).
Right now, while playing with the scaffold generator (yes, I use it, because I like they way I can quickly set up a prototype while I am still researching my objectives), I like to use a DRY method.
In my opinion, I should build a custom "object", that can hold two variables (Fixnum), one for the whole, one for the cents.
In my big dream, I would be able to do the following:
script/generate scaffold Cake name:string description:text cost:mycustom
Where mycustom should create two integer columns (one for wholes, one for cents). Right now I could do this by doing:
script/generate scaffold Cake name:string description:text cost_w:integer cost_c:integer
I had also had an idea that would be creating a "cost model", which would hold two columns of integers and create a cost_id column to my scaffold. But wouldn't that be an extra table that would cause some kind of performance penalty? And wouldn't that be defy the purpose of the Cake model in the first place, because the costs are an attribute of individual Cake entries?
The reason why I would want to have such a functionality because I am thinking of having multiple "costs" inside my rails application.
Thank you for your feedback, comments and answers! I hope my message got through as understandable, my apologies for incorrect grammar or weird sentences as English is not my native language.
© Stack Overflow or respective owner