Ruby on rails 为未命名为“0”的数据库设置主键:id";
我使用的是:Rails2.3.5Ruby1.8.7和Windows7HomeBasic 我得到了一个数据库,并将其连接到rails,从中读取和获取数据没有问题。现在我想做的是在其中添加一些功能(添加、编辑和删除),但当我尝试通过执行以下代码将主键设置为表的主键(ProductCode)时:Ruby on rails 为未命名为“0”的数据库设置主键:id";,ruby-on-rails,ruby-on-rails-2,Ruby On Rails,Ruby On Rails 2,我使用的是:Rails2.3.5Ruby1.8.7和Windows7HomeBasic 我得到了一个数据库,并将其连接到rails,从中读取和获取数据没有问题。现在我想做的是在其中添加一些功能(添加、编辑和删除),但当我尝试通过执行以下代码将主键设置为表的主键(ProductCode)时: class Product < ActiveRecord::Base self.primary_key :ProductCode end self.primary\u key返回rails当前认为是主
class Product < ActiveRecord::Base
self.primary_key :ProductCode
end
self.primary\u key
返回rails当前认为是主键的内容,因此不接受任何参数。如果要设置主键,请使用self.primary\u key='blah'
早期版本的rails也支持
设置主键的“blah”
,但在rails 3.2中,这一点被弃用。设置主键列的名称
self.primary\u key=“product\u code”请注意:“ProductCode”不是rails惯用的列名。应该是
product\u code
。但它是我数据库中的列名。我是否应该将其重命名为“product_code”?嗯,是的,我会的。这不会影响性能。但它可能会在某些地方给您带来麻烦,因为Rails鼓吹“约定优先于配置”,并假设了许多事情。其中一个问题是数据库名称在snake_的情况下。仍然没有解决argumentError:(是的,这就是为什么它是一个提示,而不是答案:)
class PosController < ApplicationController
def index
@cards = Card.find(:all)
@products = Product.find(:all, :limit => 10)
end
def new
@pro = Product.new
end
def edit
@pro = Product.find(params[:id])
end
def update
@pro = Product.find(params[:id])
if session[:user_id]
@log = "Welcome Administrator!"
@logout="logout"
else
@log = "Admin Log in"
@logout=""
end
respond_to do |format|
if @pro.update_attributes(params[:product])
flash[:notice] = 'product was successfully updated.'
format.html { redirect_to(:controller => "pos", :action => "index") }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @pro.errors, :status => :unprocessable_entity }
end
end
end
def create
@pro = Product.new(params[:product])
respond_to do |format|
if @pro.save
flash[:notice] = 'product was successfully created.'
format.html {redirect_to (:controller => "pos", :action => "index")}
#format.xml { render :xml => @product, :status => :created, :location => @product }
else
format.html { render :controller => "pos",:action => "new" }
#format.xml { render :xml => @product.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@pro = Product.find(params[:id])
@pro.destroy
respond_to do |format|
flash[:notice] = 'product was successfully deleted.'
format.html { redirect_to(:controller => "pos", :action => "index") }
format.xml { head :ok }
end
end
end