mysql - 事件模型在 new 和 create 方法调用中插入 NULL 值

标签 mysql ruby-on-rails ruby ruby-on-rails-3 activemodel

这是我在笔记模型的 create_note 方法调用上的 webrick 转储

Parameters: {"utf8"=>"✓", "authenticity_token"=>"T/F/oZaUYHz7G3HUjVKDs+Qjx+hrg6VqU4t1vr14ACc=", "note"=>{"notename"=>"Hello World 3", "notecontent"=>"3rd time hello world"}, "commit"=>"Create"}
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `notes` (`created_at`, `notecontent`, `notename`, `updated_at`, `user_id`) VALUES ('2012-01-30 07:04:31', NULL, NULL, '2012-01-30 07:04:31', 1)
   (3.3ms)  COMMIT
   (0.1ms)  BEGIN
   (0.1ms)  COMMIT
  Rendered note/create.html.erb within layouts/application (2.5ms)
Completed 200 OK in 58ms (Views: 13.4ms | ActiveRecord: 5.8ms)

See that the insert coming with NULL values for notecontent and notename.

我的模型文件是

class Note < ActiveRecord::Base
        self.table_name = "notes"
        validates_length_of :notename, :within => 5..50, :message => "Notename 5-50 chars"
        validates_length_of :notecontent, :within => 5..50, :message => "Notecontent 5-50 chars"
        attr_protected :id
        belongs_to :user

        attr_accessor :notename, :notecontent, :created_at, :updated_at

        def self.get_notes_by_user(id)
                usernotes =  Note.find :all, :conditions => ["user_id = ?", id]
                return usernotes
        end


        def self.create_note(name, content, user)
                n = Note.create(:notename => name , :notecontent => content, :user_id => user)
                if n.save!
                        return true
                else
                        return false
                end
        end

        def self.update_note (id, content)
                n = Note.find :first, :conditions => ["id = ?", id]
                n.notecontent = content;
                n.updated_at = 0.hour.from_now
        end
end

我的 Controller 文件是

class NoteController < ApplicationController

        #before_filter :login_required
  def create
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        if request.post?
                @nname = params[:note][:notename]
                @ncontent = params[:note][:notecontent]
                @uid = session[:user].getid
                @ins = params.inspect
                status = Note.create_note @nname, @ncontent, @uid
                if status == false
                        flash[:warning] = "Error While Creating Note"
                else
                        flash[:notice] =  "Note Successfully Created"
                end
        end
  end

  def delete
  end

  def show
  end

  def index
        if session[:user] == nil
                redirect_to :controller => "user", :action => "login"
        end
        user = session[:user]
        @uname = user.getname
        #nn = Note.new
        @user_notes =  Note.get_notes_by_user(user.getid)
  end
end

我已检查是否正在为注释模型中的 create_note 方法中的参数填充值。但不知何故,它似​​乎无法将这些值映射到数据库查询语言中。请帮忙。

我的迁移文件是

class AddUidToNotes < ActiveRecord::Migration
  def self.up
        add_column :notes, :user_id, :int
  end
end

class CreateNotes < ActiveRecord::Migration
  def self.up
    create_table :notes do |t|
        t.column :notename, :string
        t.column :notecontent, :string
      t.timestamps
    end
  end

  def self.down
    drop_table :notes
  end
end

数据库控制台

mysql> desc notes;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| notename    | varchar(255) | YES  |     | NULL    |                |
| notecontent | varchar(255) | YES  |     | NULL    |                |
| created_at  | datetime     | NO   |     | NULL    |                |
| updated_at  | datetime     | NO   |     | NULL    |                |
| user_id     | int(11)      | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

最佳答案

切勿将 attr_accessor 与数据库支持的属性一起使用。这将覆盖 ActiveRecord 提供的一组单独的访问器,这些访问器不会写入 ActiveRecord 使用的存储。

不要与 attr_accessible 混淆,它是批量分配保护系统的一部分,绝对可以使用

关于mysql - 事件模型在 new 和 create 方法调用中插入 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060578/

相关文章:

mysql - 按字段内出现的频率排序

php - mysql如何进行多表操作?

php - 如何修复 "Syntax error or access violation: 1064"

ruby-on-rails - Rails 3 HTTP-header If-Modified-Since 使用 curl

ruby-on-rails - mysql2 gem 0.3.15 提供编码设置为 "utf8"的 ASCII-8BIT

php - 如何同时执行多个MySql查询?

javascript - 从 JSON API 响应获取属性的唯一值计数

ruby-on-rails - rails rspec -(第二次测试)预期响应为 < :redirect>, 但为 <200>

ruby-on-rails - 我如何在 ruby​​ on rails 中修复未定义的方法 `alias_method_chain' 错误?

ruby-on-rails - RSpec 异常后继续