Ruby on rails 关于RoR更新数据库记录

Ruby on rails 关于RoR更新数据库记录,ruby-on-rails,ruby,mysql,Ruby On Rails,Ruby,Mysql,我想使用RubyonRails和mysql编写一个简单的测试程序 但是,在这段时间内,我无法更新数据库记录。 因为我用“mid”替换了“id”列 在这种情况下,如何更新记录 问题: Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL def update

我想使用RubyonRails和mysql编写一个简单的测试程序 但是,在这段时间内,我无法更新数据库记录。 因为我用“mid”替换了“id”列 在这种情况下,如何更新记录

问题:

Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL
def update
    @message = Message.find(:first, :conditions => ["mid = ?", params[:id]])
    if @message.update_attributes(params[:message])
        flash[:notice] = 'Post was successfully updated.'
        redirect_to gbook_path(:id => @message.mid)
    else
        format.html { render :action => "edit" }
    end
end
CREATE TABLE `demo_development`.`message` (
`mid` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`subject` VARCHAR( 30 ) NOT NULL ,
`author` VARCHAR( 30 ) NOT NULL ,
`message` TEXT NOT NULL ,
`adddate` INT UNSIGNED NOT NULL
) ENGINE = MYISAM ;
我的代码:

Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL
def update
    @message = Message.find(:first, :conditions => ["mid = ?", params[:id]])
    if @message.update_attributes(params[:message])
        flash[:notice] = 'Post was successfully updated.'
        redirect_to gbook_path(:id => @message.mid)
    else
        format.html { render :action => "edit" }
    end
end
CREATE TABLE `demo_development`.`message` (
`mid` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`subject` VARCHAR( 30 ) NOT NULL ,
`author` VARCHAR( 30 ) NOT NULL ,
`message` TEXT NOT NULL ,
`adddate` INT UNSIGNED NOT NULL
) ENGINE = MYISAM ;
数据库:

Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL
def update
    @message = Message.find(:first, :conditions => ["mid = ?", params[:id]])
    if @message.update_attributes(params[:message])
        flash[:notice] = 'Post was successfully updated.'
        redirect_to gbook_path(:id => @message.mid)
    else
        format.html { render :action => "edit" }
    end
end
CREATE TABLE `demo_development`.`message` (
`mid` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`subject` VARCHAR( 30 ) NOT NULL ,
`author` VARCHAR( 30 ) NOT NULL ,
`message` TEXT NOT NULL ,
`adddate` INT UNSIGNED NOT NULL
) ENGINE = MYISAM ;

您需要告诉ActiveRecord您的主键不是“id”的约定

使用“设置主键”方法

class用户

此处记录:

您需要告诉ActiveRecord您的主键不是“id”约定

使用“设置主键”方法

class用户
此处记录: