I never managed to find a good way to do this, so I thought I'd ask.
For example, in an ActiveRecord model, the attributes supported by the database are automatically converted by type to the corresponding database types. If I have a Category model and it has several attributes, say name and category_id , if I like this:
Category.new(params[:category])
Rails knows that name is a string, and category_id is an integer.
Let's say I have some transient / synthetic attributes that I want to test, and they have certain types. I want to send them from a form, and I would like them to automatically convert to String or Integer or Date (for example) depending on how they are defined.
If I were to declare something in a Rails model, for example:
attr_accessor :some_number_variable attr_accessor :some_date
Is there a built-in way to tell Rails βI would like you to port the former to Integer and the latter to Date when I go Category.new(params[:category]) β while params[:category][:some_number_variable] and params[:category][:some_date] are part of the data presented to the controller (I understand that the Date example can be a little more complicated considering the many date formats there).
source share