Rails创建不永久保存

时间:2013-07-09 18:54:17

标签: ruby-on-rails postgresql

我有一个rails 3.2.13,postgres,ruby 1.9.3应用程序,当一个对象暂时保存,然后消失时,我遇到了问题。

在我的rails控制台中,我执行了以下操作:

s = Shop.create( name: "my shop", city:"NY", state: "NY", user_id: 2)
(0.4ms)  BEGIN
SQL (1.0ms)  INSERT INTO "shops" ("about", "address1", "address2", "city", "created_at", "image", "name", "state", "stripe_shop_token", "updated_at", "user_id", "zip") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING "id"  [["about", nil], ["address1", nil], ["address2", nil], ["city", "NY"], ["created_at", Tue, 09 Jul 2013 18:39:40 UTC +00:00], ["image", nil], ["name", "my shop"], ["state", "NY"], ["stripe_shop_token", nil], ["updated_at", Tue, 09 Jul 2013 18:39:40 UTC +00:00], ["user_id", 2], ["zip", nil]]
(0.6ms)  COMMIT
=> #<Shop id: 46, user_id: 2, name: "my shop", address1: nil, address2: nil, city: "NY", state: "NY", zip: nil, about: nil, image: nil, stripe_shop_token: nil, created_at: "2013-07-09 18:39:40", updated_at: "2013-07-09 18:39:40">

Shop.all
Shop Load (1.0ms)  SELECT "shops".* FROM "shops" 
=> [#<Shop id: 46, user_id: 2, name: "my shop", address1: nil, address2: nil, city: "NY", state: "NY", zip: nil, about: nil, image: nil, stripe_shop_token: nil, created_at: "2013-07-09 18:39:40", updated_at: "2013-07-09 18:39:40">]

商店似乎保存在控制台中。我关闭了我的电脑,然后能够在控制台中恢复商店。如果我重新启动应用程序,商店似乎仍然被识别,但一旦我在应用程序中执行任何操作,它就再也找不到商店了。如果我直接进入商店视图,它将显示商店,直到我更换页面,然后商店再也找不到。

以下是一些服务器日志文件:

15:00:12 web.1  | Started POST "/__better_errors/2165427460/variables" for 127.0.0.1 at 2013-07-09 14:51:59 -0400
15:00:12 web.1  | Started GET "/" for 127.0.0.1 at 2013-07-09 14:57:45 -0400
15:00:12 web.1  | Processing by HomeController#index as HTML
15:00:12 web.1  |   Rendered home/index.html.erb within layouts/application (1.5ms)
15:00:12 web.1  |   User Load (1.1ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'ZN0O_btNVpCoktAOBlM3OQ' LIMIT 1
15:00:12 web.1  |   Shop Load (0.5ms)  SELECT "shops".* FROM "shops" WHERE "shops"."user_id" = 2 LIMIT 1
15:00:12 web.1  |   Rendered layouts/_header.html.erb (5.7ms)
15:00:12 web.1  |   Rendered layouts/_error.html.erb (0.1ms)
15:00:12 web.1  |   Rendered layouts/_footer.html.erb (0.6ms)


15:00:12 web.1  | Processing by HomeController#index as HTML
15:00:12 web.1  |   Rendered home/index.html.erb within layouts/application (1.2ms)
15:00:12 web.1  |   User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = 'ZN0O_btNVpCoktAOBlM3OQ' LIMIT 1
15:00:12 web.1  |   Shop Load (0.9ms)  SELECT "shops".* FROM "shops" WHERE "shops"."user_id" = 2 LIMIT 1
15:00:12 web.1  |   Rendered layouts/_header.html.erb (6.7ms)
15:00:12 web.1  |   Rendered layouts/_error.html.erb (0.1ms)
15:00:12 web.1  |    (0.2ms)  BEGIN
15:00:12 web.1  |   CartOrder Load (0.4ms)  SELECT "cart_orders".* FROM "cart_orders" WHERE "cart_orders"."shop_id" = 49
15:00:12 web.1  |   Item Load (0.4ms)  SELECT "items".* FROM "items" WHERE "items"."shop_id" = 49
15:00:12 web.1  |   SQL (0.4ms)  DELETE FROM "shops" WHERE "shops"."id" = $1  [["id", 49]]
15:00:12 web.1  |    (0.6ms)  COMMIT
15:00:12 web.1  |   Rendered layouts/_footer.html.erb (7.6ms)
15:00:12 web.1  | Completed 200 OK in 86ms (Views: 82.0ms | ActiveRecord: 3.6ms)

我很困难,我甚至不确定哪些信息会有所帮助。

由于 史蒂夫

2 个答案:

答案 0 :(得分:1)

尝试:

s = Shop.new( name: "my shop", city:"NY", state: "NY", user_id: 2)
s.save

答案 1 :(得分:0)

您可能会覆盖user_id=2可能拥有的数据。尝试使用不同的user_id创建多个记录,看看会发生什么。我认为user_id可能是您表格中的索引,这意味着当您使用user_id=2保存记录时,您正在编写该记录。