Search code examples
ruby-on-railsruby-on-rails-6rails-migrationsrails-models

Rails model default value being ignored when new record created?


I have a Message model like so

class CreateMessages < ActiveRecord::Migration[6.0]
  def change
    create_table :messages do |t|
      t.text :body
      t.references :conversation, null: false, foreign_key: true
      t.references :user, null: false, foreign_key: true
      t.boolean :seen, default: false
      t.timestamps
    end
  end
end

I expected default: false to set the value of 'seen' to false whenever a new message is created

But when I try

Message.create(conversation_id: 1, body: "something new!", user_id: 1)

Then seen: nil. Whereas I was expecting to see seen: false

Have I done something wrong / how can I get the desired behaviour?


Solution

  • You can reload your console by running the bellow command in your rails console or you can close and reopen your console once. I think, it'll solve your issue. Other than codes are looking good to me.

    reload!