0

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

但我发现有很多项目具有相同的 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)
4

1 回答 1

0

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

  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。

于 2013-12-01T13:00:00.310 回答