具体:
http://stackoverflow.com/questions/1200568/using-rails-how-can-i-set-my-primary-key-to-not-be-an-integer-typed-column/1434819#1434819
1.在迁移文件中加入:
create_table :users,{:id => false} do |t|
t.integer :iid
t.string :name
t.timestamps
end
execute "ALTER TABLE users ADD PRIMARY KEY (iid);"
不生成id,代替自己生成的id,然后执行sql
2.
在model中加入set_primary_key重新设置主键,然后添加before_create 用于回调User.create 函数生成id,User.new出来的是可以自己设置id的
class User < ActiveRecord::Base
set_primary_key :iid;
before_create :init_iid;
def init_iid
self.iid = ;
end
end
分享到:
相关推荐
Rails3教程系列 DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY(Dont Repeat Yourself) DRY...
Ruby on Rails中文指南
Rails 3 in Action 2011年9月新鲜出炉,针对最新的Rails 3.1进行说明
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置
turbo-sprockets-rails3, 加速你的Rails 3资产 用于 Rails 3.2.x的涡轮链轮 通过只根据源文件的哈希来重新编译已经更改的资产,从而加快 Rails 3 rake assets:precompile的速度只编译一次以生成指纹和非打印的资产...
Ruby+on+Rails+3+Tutorial.pdf 应用Rails进行敏捷Web开发第4版.pdf (Agile Web Development with Rails) Rails.Recipes.Rails.3.Edition.pdf
rails_semantic_logger, Rails 语义记录器用语义记录器替换 Rails 缺省记录器 Rails 语义记录器 语义记录器用语义记录器替代 Rails 缺省记录器。http://github.com/rocketjob/rails_semantic_logger文档有关完整文档...
Rails3消息队列系统 Sidekiq
The Rails™ 3 Way is a comprehensive resource that digs into the new features in Rails 3 and perhaps more importantly, the rationale behind them. —Yehuda Katz, Rails Core The Bible for Ruby on ...
Rails 3中的Active Record的查询变化.doc
Rails3的ActiveRecord 查询API.doc
3 rails # gem install rails -v=2.3.11 4 passenger # gem install passenger passenger-install-apache2-module 安装完成后按照提示将下面的内容添加到etc/httpd/httpd.conf LoadModule pa
rails3的英文文档
The Rails™ 3 Way is the only comprehensive, authoritative guide to delivering production-quality code with Rails 3. Pioneering Rails expert Obie Fernandez and a team of leading experts illuminate ...
rails常用命令,例如新建rails项目,新建controller、model 等等
基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip 基于java的开发源码-Rails3消息队列系统 Sidekiq.zip ...
Record Cache - 在Rails 3中缓存Active Model Records
ruby on rails开发的初学者很适合的工具书,易上手,很实用,
rails指南 中文版