Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 8th, 2012  |  syntax: None  |  size: 2.98 KB  |  hits: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ruby Rails Database
  2. tenant = Tenant.new
  3. => #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
  4. >> first_name = "Billy"
  5. => "Billy"
  6. >> last_name = "Jones"
  7. => "Jones"
  8. >> tenant.save
  9.    (0.1ms)  BEGIN
  10.   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')`  
  11. 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')
  12.    (0.1ms)  ROLLBACK
  13. 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')
  14.  
  15. 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?
  16.  
  17.  
  18. Below are controller/model
  19.  
  20.  
  21.  
  22. ***** TENANT CONTROLLER
  23. class TenantsController < ApplicationController
  24.  
  25.     def index
  26.         render('list')
  27.     end
  28.  
  29.     def list
  30.       @tenants = Tenant.order("tenants.last_name ASC")
  31.     end
  32.  
  33.     def show
  34.       @tenant = Tenant.find(params[:id])
  35.     end
  36.  
  37.     def new
  38.       @tenant = Tenant.new
  39.     end
  40.  
  41.     def create
  42.         @tenant = Tenant.new(params[:tenant])
  43.         @tenant.save
  44.  
  45.         if @tenant.save
  46.             flash[:notice] = "New Tenant was created successfully."
  47.             redirect_to(:action => 'list')
  48.         else
  49.             render('new')
  50.         end
  51.     end
  52.  
  53.     def edit
  54.       @tenant = Tenant.find(params[:id])
  55.  
  56.     end
  57.  
  58.  
  59.     def update
  60.         # find object using form parameters
  61.         @tenant = Tenant.find(params[:id])
  62.  
  63.         #update the object
  64.         if @tenant.update_attributes(params[:tenant])
  65.  
  66.             #if update succeeds redirect to
  67.             flash[:notice] = "Tenant was updated successfully."
  68.             redirect_to(:action => 'show', :id => @tenant.id)
  69.             else
  70.             render('edit')
  71.         end
  72.  
  73.     end
  74.  
  75.     def delete
  76.         @tenant = Tenant.find(params[:id])
  77.  
  78.     end
  79.  
  80.     def destroy
  81.         @tenant = Tenant.find(params[:id])
  82.         @tenant.destroy
  83.  
  84.         flash[:notice] = "Tenant was destroyed successfully."
  85.         redirect_to(:action => 'list' )
  86.  
  87.     end
  88.  
  89.  
  90. end
  91.  
  92. ***** TENANT MODEL
  93. class Tenant < ActiveRecord::Base
  94.     attr_accessible :first_name, :last_name, :home_id
  95. end
  96.        
  97. tenant = Tenant.new
  98. => #<Tenant id: nil, first_name: nil, last_name: nil, home_id: nil, created_at: nil, updated_at: nil>
  99. >> tenant.first_name = "Billy"
  100. => "Billy"
  101. >> tenant.last_name = "Jones"
  102. => "Jones"
  103. >> tenant.save
  104.        
  105. tenant = Tenant.create(:first_name => "Billy", :last_name => "Jones")
  106.        
  107. first_name = "Billy"
  108.        
  109. tenant.first_name = "Billy"
  110.        
  111. puts tenant.errors.full_errors