Rails using time_select in an inactive write model

I am trying to use time_select to input time into a model that will then do some calculations.

the time_select helper prepares the return parameters so that it can be used when assigning an Active Record object with multiple parameters.

Something like the following

Parameters: {"commit"=>"Calculate", "authenticity_token"=>"eQ/wixLHfrboPd/Ol5IkhQ4lENpt9vc4j0PcIw0Iy/M=", "calculator"=>{"time(2i)"=>"6", "time(3i)"=>"10", "time(4i)"=>"17", "time(5i)"=>"15", "time(1i)"=>"2009"}} 

My question is what is the best way to use this format in an inactive record model. Also on a side note. What is the meaning of (5i), (4i), etc.? (Except for the obvious reason to distinguish between different times, mainly why it was named that way)

thanks

+4
source share
2 answers

You can create a method in an inactive recording model as follows

 # This will return a Time object from provided hash def parse_calculator_time(hash) Time.parse("#{hash['time1i']}-#{hash['time2i']}-#{hash['time3i']} #{hash['time4i']}:#{hash['time5i']}") end 

Then you can call the method from the controller action as follows

 time_object = YourModel.parse_calculator_time(params[:calculator]) 

This may not be the best solution, but it is easy to use.

Greetings :)

+4
source

The letter after the number indicates the type to which you want to distinguish it. In this case, integer . It can also be f for float or s for string .

I just did it myself, and the easiest way I could find was basically copying / pasting Rails code into my base module (or abstract object).

I copied the following functions verbatim from ActiveRecord::Base

  • assign_multiparameter_attributes(pairs)
  • extract_callstack_for_multiparameter_attributes(pairs)
  • type_cast_attribute_value(multiparameter_name, value)
  • find_parameter_position(multiparameter_name)

I also have the following methods that call / use them:

 def setup_parameters(params = {}) new_params = {} multi_parameter_attributes = [] params.each do |k,v| if k.to_s.include?("(") multi_parameter_attributes << [ k.to_s, v ] else new_params[k.to_s] = v end end new_params.merge(assign_multiparameter_attributes(multi_parameter_attributes)) end # Very simplified version of the ActiveRecord::Base method that handles only dates/times def execute_callstack_for_multiparameter_attributes(callstack) attributes = {} callstack.each do |name, values| if values.empty? send(name + '=', nil) else value = case values.size when 2 then t = Time.new; Time.local(t.year, t.month, t.day, values[0], values[min], 0, 0) when 5 then t = Time.time_with_datetime_fallback(:local, *values) when 3 then Date.new(*values) else nil end attributes[name.to_s] = value end end attributes end 

If you find a better solution, let me know :-)

+2
source

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


All Articles