- Ruby Rails Database
- tenant = Tenant.new
- => #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
- >> first_name = "Billy"
- => "Billy"
- >> last_name = "Jones"
- => "Jones"
- >> tenant.save
- (0.1ms) BEGIN
- SQL (0.2ms) INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`, `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')`
- Mysql2::Error: Column 'first_name' cannot be null: INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`, `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')
- (0.1ms) ROLLBACK
- ActiveRecord::StatementInvalid: Mysql2::Error: Column 'first_name' cannot be null: INSERT INTO `tenants` (`created_at`, `first_name`, `home_id`, `last_name`, `updated_at`) VALUES ('2012-07-29 20:54:28', NULL, NULL, NULL, '2012-07-29 20:54:28')
- SO, I cannot determine the problem. It says first_name cannot be null, obviously because I setup the database that way, but there is a value trying to go into database, even though I can return a value in Rails Console asking for first_name..... Any advise?
- Below are controller/model
- ***** TENANT CONTROLLER
- class TenantsController < ApplicationController
- def index
- render('list')
- end
- def list
- @tenants = Tenant.order("tenants.last_name ASC")
- end
- def show
- @tenant = Tenant.find(params[:id])
- end
- def new
- @tenant = Tenant.new
- end
- def create
- @tenant = Tenant.new(params[:tenant])
- @tenant.save
- if @tenant.save
- flash[:notice] = "New Tenant was created successfully."
- redirect_to(:action => 'list')
- else
- render('new')
- end
- end
- def edit
- @tenant = Tenant.find(params[:id])
- end
- def update
- # find object using form parameters
- @tenant = Tenant.find(params[:id])
- #update the object
- if @tenant.update_attributes(params[:tenant])
- #if update succeeds redirect to
- flash[:notice] = "Tenant was updated successfully."
- redirect_to(:action => 'show', :id => @tenant.id)
- else
- render('edit')
- end
- end
- def delete
- @tenant = Tenant.find(params[:id])
- end
- def destroy
- @tenant = Tenant.find(params[:id])
- @tenant.destroy
- flash[:notice] = "Tenant was destroyed successfully."
- redirect_to(:action => 'list' )
- end
- end
- ***** TENANT MODEL
- class Tenant < ActiveRecord::Base
- attr_accessible :first_name, :last_name, :home_id
- end
- tenant = Tenant.new
- => #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
- >> tenant.first_name = "Billy"
- => "Billy"
- >> tenant.last_name = "Jones"
- => "Jones"
- >> tenant.save
- tenant = Tenant.create(:first_name => "Billy", :last_name => "Jones")
- first_name = "Billy"
- tenant.first_name = "Billy"
- puts tenant.errors.full_errors