views:

145

answers:

1

I would like to use the ruby on rails and mysql to write a simple test program but, in this time, i can not update the database record. because i use the "mid" to replaced the "id" column in this case, what can i do to work to update the record?

Problem:

Mysql::Error: Unknown column 'id' in 'where clause': UPDATE `messages` SET `message` = 'ccc', `subject` = 'aaa', `author` = 'bbb' WHERE `id` = NULL

My Code:

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

Database:

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 ;
+5  A: 

You need to tell ActiveRecord that your primary key is not the convention of 'id'.

Use the "set_primary_key" method.

class User < ActiveRecord::Base
  set_primary_key :mid
end

Documented here: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002290

Cody Caughlan