3
使用rails 3.2.3,ruby 1.9.3和postgres 8.4.12(在RHEL 6.3上)的Im。我希望試圖從控制器保存該記錄時,IP地址存儲在數據庫中的整數,但出現以下錯誤:如何在Rails 3和Postgres中處理大整數
def create
@network = Network.new(params[:network])
@network.address_int = IPAddr.new(@network.address_str).to_i
@network.netmask_int = IPAddr.new(@network.netmask_str).to_i
respond_to do |format|
if @network.save
format.html { redirect_to @network, notice: 'Network was successfully created.' }
format.json { render json: @network, status: :created, location: @network }
else
format.html { render action: "new" }
format.json { render json: @network.errors, status: :unprocessable_entity }
end
end
end
我收到以下錯誤
我的遷移定義本欄目爲8字節整數:
class CreateNetworks < ActiveRecord::Migration
def change
create_table :networks do |t|
t.integer :vlan
t.integer :address_int, :limit => 8
t.integer :netmask_int, :limit => 8
t.string :address_str
t.string :netmask_str
t.string :desc
t.timestamps
end
end
end
但我已經能夠這樣大整數存儲在軌道控制檯:
1.9.3p125 :001 > n=Network.new
=> #<Network id: nil, vlan: nil, address_int: nil, address_str: nil, netmask_int: nil, netmask_str: nil, desc: nil, created_at: nil, updated_at: nil>
1.9.3p125 :002 > n.address_int=4294967040
=> 4294967040
1.9.3p125 :003 > n
=> #<Network id: nil, vlan: nil, address_int: 4294967040, address_str: nil, netmask_int: nil, netmask_str: nil, desc: nil, created_at: nil, updated_at: nil>
1.9.3p125 :004 > n.save
(0.2ms) BEGIN
SQL (5.0ms) INSERT INTO "networks" ("address_int", "address_str", "created_at", "desc", "netmask_int", "netmask_str", "updated_at", "vlan") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["address_int", 4294967040], ["address_str", nil], ["created_at", Tue, 10 Jul 2012 15:09:37 UTC +00:00], ["desc", nil], ["netmask_int", nil], ["netmask_str", nil], ["updated_at", Tue, 10 Jul 2012 15:09:37 UTC +00:00], ["vlan", nil]]
(0.9ms) COMMIT
=> true
那麼,這個bigint數據類型應該定義在別的地方嗎?提前致謝。
任何理由不使用字符串? – AJcodez 2012-07-11 09:28:01
是的,Id而不是存儲一個點分十進制整數作爲一個字符串。我會在上面做數學運算,並將它存儲爲一個字符串,只是感覺很髒。 – Sirch 2012-07-11 09:34:38
http://stackoverflow.com/questions/10055499/how-do-you-run-rails-runner-in-heroku – PriteshJ 2012-07-11 11:24:48