小编Zeu*_*Zeu的帖子

关于RoR更新数据库记录

我想在rails和mysql上使用ruby编写一个简单的测试程序但是,在这个时候,我无法更新数据库记录.因为在这种情况下我使用"mid"替换"id"列,我可以做些什么来更新记录呢?

问题:

Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL
Run Code Online (Sandbox Code Playgroud)

我的代码:

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
Run Code Online (Sandbox Code Playgroud)

数据库:

CREATE TABLE `demo_development`.`message` (
`mid` MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`subject` VARCHAR( 30 ) NOT NULL , …
Run Code Online (Sandbox Code Playgroud)

ruby mysql ruby-on-rails

0
推荐指数
1
解决办法
571
查看次数

标签 统计

mysql ×1

ruby ×1

ruby-on-rails ×1