In Rails, is there a way to tell TYPE what attr_accessor should be and check it using the built-in validation of Rails?

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).

+4
source share
5 answers

None of the answers answer the question. I believe that the correct answer to the questions is simply "No." The answers both guide possible workarounds to the fact that the answer to the question is β€œNo,” and both are valid comments / ideas.

+1
source

attr_accesor just creates read / write methods, and it comes from Ruby , not Rails. The Rails method, which I think is often confused, is attr_accessible , which serves another purpose . If you want to use when an attribute is being read, you can simply override the reader.

 attr_accessor :some_number_variable def some_number_variable @some_number_variable.to_i end 

This will at least give you a writer for free. This is the best solution that I know for what (I think) you are describing.

EDIT. Regarding validation, I believe that if you use Rails 3, it will be much easier for you to check for these attributes (I did not, so I can’t say for sure). Now that the checks are not tied directly to ActiveRecord, I believe that this is possible.

0
source

I assume that the transition / synthetic attribute means a virtual attribute?

If so, there is no built-in Rails function that will do this, which I can think carelessly, but one solution would be to simply write a module for these attributes and include it in the model. Or, if it should be dynamic, you can call an attribute something like: some_variable_integer, and then process these dynamic attributes with the method_missing method, read the method name and convert it to the required type.

In my opinion, this is a little dirty, so validations in the model are really best if what you want to do can support them.

0
source

use before_create ..

 class Category attr_accessor :some_number_variable, :some_date before_create :validate private def validate return unless !!params[:category] #do variable casting here end end 
0
source

Add to your model:

 columns_hash["birth_date"] = ActiveRecord::ConnectionAdapters::Column.new("birth_date", nil, "date") 

reference: Rails ActiveRecord :: MultiparameterAssignmentErrors

0
source

Source: https://habr.com/ru/post/1345594/


All Articles