Ayanami
Ayanami

Reputation: 85

New strong parameter not being saved in database

I added a new column named "version" to a table in Rails using a migration and manually added the corresponding parameter to the strong parameters permitted in the corresponding controller:

def endpoint_params
params.require(:endpoint).permit(:hostname, :username, :password, :connection_string, :entity_id, :created_at,
                                 :updated_at, :endpoint_type_id, :endpoint_app_id, :environment_id, :created_by,
                                 :updated_by, :version)
end

However, when I try to set that new parameter in the update action and save it to the database, it doesn't get saved at all. The code that I'm using is:

def update

begin

  @environment = Environment.find(params[:env])

  version_no = (EndpointsFile.where("environment_id = ?", @environment.id).maximum('version') || 0) + 1

  @endpoint.updated_by = current_user.id
  @endpoint.version = version_no

  respond_to do |format|

    if @endpoint.update(endpoint_params)

      @endpoints = Endpoint.where("environment_id = ? and id <> ?", @environment.id, @endpoint.id)

      EndpointsFile.create!(version: version_no, date: Time.now, environment_id: @environment.id)

      @endpoints.each do |e|

        e.version = version_no
        e.save

        puts "e.version: #{e.version}"   **=> HERE IT PRINTS e.version: and the field is null in the database**

      end

      format.html { redirect_to env_endpoints_path(env: @environment.id), notice: t('.notice') }
      format.json { render :show, status: :ok, location: @endpoint }
    else
      format.html { render :edit }
      format.json { render json: @endpoint.errors, status: :unprocessable_entity }
    end
  end

  rescue => exception

    logger.error { "endpoints_controller.update -> Ocorreu um erro ao atualizar o endpoint: #{exception.message}" }

    respond_to do |format|
       format.html { redirect_to env_endpoints_path(env: @environment.id), alert: "Ocorreu um erro ao atualizar o endpoint: #{exception.message}" and return }
       format.json { render json: @endpoint.errors, status: :unprocessable_entity }
    end

  end
end

This is the Endpoint model:

class Endpoint < ApplicationRecord

 belongs_to :entity
 belongs_to :environment
 belongs_to :endpoint_app
 belongs_to :endpoint_type

 has_many :configs_histories

 has_paper_trail

end

The table in DB is:

create_table "endpoints", force: :cascade do |t|
   t.string   "hostname"
   t.string   "username"
   t.string   "password"
   t.string   "connection_string"
   t.integer  "entity_id"
   t.datetime "created_at",        null: false
   t.datetime "updated_at",        null: false
   t.integer  "endpoint_type_id"
   t.integer  "endpoint_app_id"
   t.integer  "environment_id"
   t.integer  "created_by"
   t.integer  "updated_by"
   t.integer  "version"
   t.index ["endpoint_app_id"], name: "index_endpoints_on_endpoint_app_id"
   t.index ["endpoint_type_id"], name: "index_endpoints_on_endpoint_type_id"
   t.index ["entity_id"], name: "index_endpoints_on_entity_id"
   t.index ["environment_id"], name: "index_endpoints_on_environment_id"
 end

What am I doing wrong? Why doesn't the command e.save work?

Upvotes: 0

Views: 112

Answers (2)

puneet18
puneet18

Reputation: 4427

Finally found the solution.

version is a reserved keyword so it can't be used as a column name.

check link: http://reservedwords.herokuapp.com/words?page=9

Upvotes: 1

Md. Farhan Memon
Md. Farhan Memon

Reputation: 6121

Since you are calculating version_no inside update, you don't have to pass it in controller or permit in strong parameters, just do the following

@endpoint.update(endpoint_params.merge(version: version_no))

Upvotes: 0

Related Questions