在Rails 3上的模型中为新项生成唯一ID

时间:2013-10-25 09:52:21

标签: ruby-on-rails ruby ruby-on-rails-3 rails-activerecord rails-postgresql

我将书籍项目填充到我的Book模型中,

但我发现有很多项具有相同的ID。

那么,如何为项目创建唯一的ID。为了防止许多商品具有相同的ID?

这是图书模型代码

# encoding: utf-8
class Book < ActiveRecord::Base
   attr_accessible :name, :isbn ,:price ,:comment ,:author ,:sale_type ,:publisher ,:sn ,:category
   attr_accessible :location, :category, :release_date
   validates_uniqueness_of :sn

以下是我的项目的一部分

irb(main):058:0> Book.all[1..10]
+-----+------+-----+-----+------+------+-----+------+-----+-----+------+------+-----+------+
| id  | pric | com | cre | upda | rele | loc | sn   | isb | aut | sale | name | cat | publ |
+-----+------+-----+-----+------+------+-----+------+-----+-----+------+------+-----+------+
| 118 | 4543 | 作  | 201 | 2013 | 2006 | --- | 2124 | 978 | 趙  | prom | 求索 | 商  | 聯經 |
| 118 | 872  | 馬  | 201 | 2013 | 2013 | --- | 2124 | 978 | 黎  | prom | 告別 | 政  | 聯經 |
| 118 | 2105 | 某  | 201 | 2013 | 2012 | --- | 2124 | 978 | 吳  | prom | 複眼 | 政  | 夏日 |
| 118 | 301  | 作  | 201 | 2013 | 2006 | --- | 2124 | 978 | 王  | norm | 天香 | 歷  | 麥田 |
| 118 | 411  | 少  | 201 | 2013 | 2008 | --- | 2124 | 978 | 韓  | norm | 鞋癖 | 商  | 聯經 |
| 119 | 3751 | 有  | 201 | 2013 | 2010 | --- | 2124 | 978 | 紀  | prom | 私家 | 體  | 印刻 |
| 119 | 3361 | 文  | 201 | 2013 | 2010 | --- | 2124 | 978 | 林  | fix_ | 我不 | 體  | 印刻 |
| 119 | 1140 | 何  | 201 | 2013 | 2012 | --- | 2124 | 978 | 邁  | norm | 正義 | 體  | 雅言 |
| 119 | 888  | 一  | 201 | 2013 | 2007 | --- | 2124 | 978 | 福  | fix_ | 生命 | 商  | 究竟 |
| 119 | 3283 | 近  | 201 | 2013 | 2011 | --- | 2124 | 978 | 芮  | norm | 海拉 | 政  | 遠流 |
+-----+------+-----+-----+------+------+-----+------+-----+-----+------+------+-----+------+

这里是生成我的数据的rake代码

 16         bk = Book.new(:sn => real_sn,:name => book_name, :isbn=>isbn,
 17                      :price =>Random.rand(200..5000), :location=>location, :category=>["商業","歷史","體育","政治"].sample,
 18                      :author => author, :sale_type => [:fix_priced, :normal, :promotion].sample, :publisher => publisher,
 19                      :release_date => rand(10.years).ago, :comment => comment
 20                      )

表格中的列我使用Postgre DB

    Column    |            Type             |                     Modifiers
--------------+-----------------------------+----------------------------------------------------
 id           | integer                     | not null default nextval('books_id_seq'::regclass)
 price        | integer                     |
 comment      | text                        |
 created_at   | timestamp without time zone | not null
 updated_at   | timestamp without time zone | not null
 release_date | text                        |
 location     | text                        |
 sn           | bigint                      |
 isbn         | bigint                      |
 author       | text                        |
 sale_type    | text                        |
 name         | text                        |
 category     | text                        |
 publisher    | text                        |
Indexes:
    "books_pkey" PRIMARY KEY, btree (id)

1 个答案:

答案 0 :(得分:0)

上面的代码,不保存数据库中的任何记录,只是实例化 Book模型的对象。您应该在初始化bk.save之后保存对象,或使用创建方法而不是新方法。

  bk = Book.new(:sn => real_sn,:name => book_name, :isbn=>isbn,
                :price =>Random.rand(200..5000), :location=>location,
                :category=>["商業","歷史","體育","政治"].sample, :author => author,
                :sale_type => [:fix_priced, :normal, :promotion].sample,
                :publisher => publisher, :release_date => rand(10.years).ago,
                :comment => comment)
  bk.save

或者您可以使用create方法

  bk = Book.create(:sn => real_sn,:name => book_name, :isbn=>isbn,
                :price =>Random.rand(200..5000), :location=>location,
                :category=>["商業","歷史","體育","政治"].sample, :author => author,
                :sale_type => [:fix_priced, :normal, :promotion].sample,
                :publisher => publisher, :release_date => rand(10.years).ago,
                :comment => comment)

保存在数据库中后,它将自动获得唯一ID。