0

I command in Terminal to change on column in database to not null, but It seems not work.

rails g migration change_column_null :Speaker, :surname, false

I got a file ChangeColumnNull But inside, it is nothing.

class ChangeColumnNull < ActiveRecord::Migration
def change
end
end

Lecture Controller (Def Create):

class CplecturesController < ApplicationController
layout 'cp_layout'

 def create
@lecture = Lecture.new(lecture_params)

@lecture.save
redirect_to @lecture
end

private
def lecture_params
  params.require(:lecture).permit(:lecture_title, :lecture_day,   :column, :start_time, :end_time, :registered_speakers, :guest_speakers, :description)
end
end

Forms

 <%= form_for :lecture, url:lectures_path do |f| %>
 <form>
 <div class="form-group">
  <%=label_tag "Lecture Title" %><br>
  <%= f.text_field :lecture_title, :class => "form-control",   :placeholder => "Example: Why is Wordpress the best?" %>
</div>
1

2 Answers 2

6

Erase that migration and write a blank migration with a better name and then fill it out by setting a default value. If you have a default value it will never be null.

rails g migration ChangeColumnOnTableName

Then inside that migration do the following:

change_column :name_of_table, :name_of_column, :data_type_of_column, :null => false

If you're only worried about it being null based on what a user enters, you could simply add a validation that requires it. In your model:

validates :name_of_column, presence: true
Sign up to request clarification or add additional context in comments.

25 Comments

va185042:project Nixon$ rails g nameNull change_column :Speaker, :organisation, :string, :default => false va185042:project Nixon$ bundle exec rake db:migrate Nothing to annotate. After I command this, nothing happen, not even file...
+1 for the validation in the model, but why use a default value instead of null: false? Wouldn't you prefer to fail if data was input without a required field, rather than storing potentially garbage data with a guessed default?
@Joe that change_column goes inside the migration. Not as part of the migration command. You run them separately. I will update to show you.
@GoGoCarl valid point. depends on why you don't want it to be null. We don't really have that information. If it's a boolean field, for instance, I think you might naturally want it to be true or false instead of enforced. But don't disagree in principle. That works, too.
As a note, modern Rails recommends validates :name_of_column, presence: true instead of the legacy style.
|
1

if you are using latest ruby (2.5+) Here is the migration script to change fields from NOT NULL to NULL

class ChangeEmailPasswordToNullableUsers < ActiveRecord::Migration[5.2] def change change_column_null :users, :email, true change_column_null :users, :password, true end end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.