ruby - Rails - Convert hours and minutes to seconds then assign value to key -


i combine both values :hours , :minutes , convert them to_i in seconds. next assign value (which should in seconds) :time_duration column in cars db before creates new service. :time_duration in hidden_field because there's no reason render data in view.

views

this _car_fields.html.erb nested partial inside view template called, _form.html.erb .

_car_fields.html.erb

<div class="nested-fields">   <div class="field">         <%= f.label :name %><br>         <%= f.text_field :name %><br>         <%= f.label :hours %>         <%= f.select :hours, '0'..'8' %>         <%= f.label :minutes %>         <%= f.select :minutes, options_for_select( (0..45).step(15), selected: f.object.minutes )%><br>         <%= f.label :price %><br>         <%= f.text_field :price, :value => (number_with_precision(f.object.price, :precision => 2) || 0) %> <br>         <%= f.label :details %><br>         <%= f.text_area :details %></div>     <%= link_to_remove_association "remove car", f, class: 'btn btn-default' %>    <%= f.hidden_field :time_duration, value: %>   <br>     <hr> </div> 

_form.html.erb

<%= simple_form_for @service |f| %>   <div class="field">      <%= f.label "select service category" %>     <br>      <%= collection_select(:service, :service_menu_id, servicemenu.all, :id, :name, {:prompt => true }) %>      <%= f.fields_for :cars |task| %>       <%= render 'car_fields', :f => task %>     <% end %>   </div>    <div class="links">     <%= link_to_add_association 'add new car', f, :cars, class: 'btn btn-default' %>   </div><br>    <div class="actions">     <%= f.submit %>   </div> <% end %> 

controller

services_controller

def new     @service = current_tech.services.build   end    def create     @service = current_tech.services.build(service_params)      respond_to |format|       if @service.save         format.html { redirect_to @service, notice: 'service created.' }         format.json { render :show, status: :created, location: @service }       else         format.html { render :new }         format.json { render json: @service.errors, status: :unprocessable_entity }       end     end   end    private      def service_params       params.require(:service).permit(:name, :service_menu_id, cars_attributes: [:tech_id, :name, :hours, :minutes, :price, :details, :_destroy])     end 

models

service.rb

class service < activerecord::base       belongs_to :tech     belongs_to :service_menu     has_many :cars, dependent: :destroy     accepts_nested_attributes_for :cars, :reject_if => :all_blank, :allow_destroy => true end 

car.rb

class car< activerecord::base   belongs_to :service   belongs_to :tech   has_many :appointments  end 

first, can remove hidden time_duration field form, since not needed.

then, you'll create before_save method car model:

car.rb

class car < activerecord::base   belongs_to :service   belongs_to :tech   has_many :appointments     before_save :generate_time_duration    def generate_time_duration     self[:time_duration] = hours.hours.to_i + minutes.minutes.to_i   end end 

what does: before car object saved, run generate_time_duration method. method sums hours.to_i , minutes.to_i , assigns car's time_duration attribute.


update old db records

since you're adding functionality in application after records have been created, here quick way update of current records:

  1. in command line, open rails console running command rails c (or rails console)
  2. in console, run command: car.all.each { |c| c.save! }

this quick, one-time fix loop through car records, save them, , subsequently update time_duration fields.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -