aarkerio
aarkerio

Reputation: 2364

Rails 5 filters fields

So, in the Rails console I'm creating a row in this way:

params = {:question=>"33333", :explanation=>"333", :hint=>"333", :worth=>1, :tags=>"3333", :active=>true, :qtype=>true, :user_id=>4}

q = Question.create! params

SQL (3.2ms)  INSERT INTO "questions" ("user_id", "question", "explanation", "hint", "tags", "worth", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["user_id", 4], ["question", "33333"], ["explanation", "333"], ["hint", "333"], ["tags", "3333"], ["worth", 1], ["created_at", 2016-09-09 20:45:51 UTC], ["updated_at", 2016-09-09 20:45:51 UTC]]

as anybody can sees, "active" and "qtype" fields are being filtered by Rails. Both columns exist in the migration file and in PostgreSQL's table. The model is pretty basic:

class Question < ApplicationRecord
   belongs_to :user
   has_many   :answer

   default_scope { order('id DESC') }

   validates :question, presence: true
end 

I don't understand why rails filters those fields. I deleted the schema.rb file and created the database from scratch but the error persists.

Upvotes: 0

Views: 150

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36880

There's been previously reported issues with booleans having a NULL value in PostgreSQL not correctly being set by rails.

It should resolve it if you re-create the fields with default value. Roll the migration back, and then recreate with...

add_column :questions, :active, :boolean, default: false
add_column :questions, :qtype, :boolean, default: false

Incidentally, the line...

has_many :answer

should probably be...

has_many :answers

Upvotes: 1

Related Questions