mardi 21 février 2017

Rails "NOT NULL constraint failed: table.column"

I have a accounts table with not null constraint on its name and number column. I also have this piece of code for "accounts" controller in create Action:

def create
    account_number = rand.to_s[2..9]
    @account = Account.new(user_id: current_user.id, name: 'hello', number: account_number)

    respond_to do |format|
        if @account.save
            format.html { redirect_to @account, notice: "Account was successfully created.\n Your account number is: #{account_number}" }

and my "Account" model is very simple:

class Account < ApplicationRecord
    belongs_to :user

    attr_accessor :name
    attr_accessor :number
    attr_accessor :amount
end

But when I go to /accounts/new and submit, it gives me this error:

NOT NULL constraint failed: accounts.name

But why? because I have hard codded the "name" attribute with "hello" in the constructor, so why it is not filling "name" column with "hello"?

Aucun commentaire:

Enregistrer un commentaire