Suavocado
Suavocado

Reputation: 969

Rails - set the hour on datetime from within controller

I have a form that includes a date-field associated with a datetime. The date is set by the user but the hour should always be 8pm UTC (20 on a 24-hour clock).

I tried setting it through my controller with

def create
    @rfq = Rfq.new(rfq_params)
    @rfq.owner = current_user.email
    @rfq.due.change(hour: 20)
    respond_to do |format|
      if @rfq.save
        format.html { redirect_to @rfq, notice: 'Rfq was successfully created.' }
        format.json { render :show, status: :created, location: @rfq }
      else
        format.html { render :new }
        format.json { render json: @rfq.errors, status: :unprocessable_entity }
      end
    end
  end

But when I save it, it always comes out to 00:00:00 UTC

Thank you ahead of time for the response

Upvotes: 0

Views: 485

Answers (1)

bobomoreno
bobomoreno

Reputation: 2858

I think you may be having the same issue as here: Ruby Rails Time.change not working as I would expect...I have checked the docs!

You are not assigning the change:

    @rfq.due = @rfq.due.change(hour: 20)

Upvotes: 1

Related Questions