deepinder
deepinder

Reputation: 131

Rails insert/ update missing a param

I am seeing this behavior for the first time. When making an insert the insert is missing one of the params. Here are my model and controller

Model

class Campaign < ApplicationRecord
  belongs_to :supplier
  has_many :campaign_offices
  has_many :campaign_plans
  has_many :campaign_plan_tiers
end

Controller

class CampaignsController < ApplicationController

  before_action :get_model, :only => [ :edit, :update, :destroy, :show ]

  def index
    @models = Campaign.order(:display_name)
  end

  def show

  end

  def create
    Campaign.create(model_params)
    redirect_to campaigns_path
  end

  def edit
    @models = Campaign.order(:display_name)
  end

  def update
    @model.update(model_params)
    redirect_to campaigns_path
  end

  def destroy
    @model.destroy
    redirect_to campaigns_path
  end


  private

  def get_model
    @model = Campaign.find(params[:id])
  end

  def model_params
    params.require(:campaign).permit( :display_name, :supplier_id, :campaign_code, :start_date, :end_date )
  end

end

And this is how the insert is working. This is a copy paste of the server logs on my local dev machine

Started POST "/campaigns" for 127.0.0.1 at 2018-04-04 13:19:44 -0400
   (0.4ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
   (0.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Processing by CampaignsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PA+xaH7ehMCNFB7+FfQ38KF2W07T7rGebj3rBQhJ6SLKqxyJTyTrES0qKDE5HWhEZ1x0p9vTO4Ur1qk4CYvPgQ==", "campaign"=>{"display_name"=>"Beat the heat", "campaign_code"=>"BTH-0293949", "supplier_id"=>"6", "start_date"=>"05/01/2018", "end_date"=>"05/31/2018"}, "commit"=>"Submit"}
   (0.2ms)  BEGIN
  Supplier Load (0.3ms)  SELECT  `suppliers`.* FROM `suppliers` WHERE `suppliers`.`id` = 6 LIMIT 1
  **SQL (0.7ms)  INSERT INTO `campaigns` (`display_name`, `supplier_id`, `campaign_code`, `start_date`, `created_at`, `updated_at`) VALUES ('Beat the heat', 6, 'BTH-0293949', '2018-01-05', '2018-04-04 17:19:44', '2018-04-04 17:19:44')**
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/campaigns
Completed 302 Found in 58ms (ActiveRecord: 3.9ms)

The end date is not making its way in either insert or update statements. It is being omitted. The column exists in db and migrations also have the column listed. I have also started the server but same thing. There are no callbacks in the model as well as evident from the model code pasted above.

Environment is mysql, rails 5.1.6, ruby 2.4.0

I have never seen this before. Any help would be appreciated.

Thanks

Upvotes: 1

Views: 229

Answers (1)

mu is too short
mu is too short

Reputation: 434745

If you look at the parameters for start_date, we see this:

"start_date"=>"05/01/2018"

and in the INSERT statement we see the start_date in ISO-8601 format:

'2018-01-05'

The input format is being interpreted as DD/MM/YYYY. If we look at the end_date, we see:

"end_date"=>"05/31/2018"

That's MM/DD/YYYY not DD/MM/YYYY. When your model tries to parse that as a date using the DD/MM/YYYY format, it gets nonsense and the date is ignored.

Your best bet is to use the ISO 8601 date format (YYYY-MM-DD) everywhere except for display. Fix the format in the client or, if necessary, in your controller.

Upvotes: 2

Related Questions