0

我有一个 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">]

商店似乎保存在控制台中。我关闭了计算机,然后能够在控制台中重新启动 Shop。如果我重新启动应用程序,商店似乎仍然可以识别,但是一旦我在应用程序中执行任何操作,它就再也找不到商店了。如果我直接进入商店视图,它会显示商店,直到我更改页面,然后再找不到商店。

这是一些服务器日志文件:

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)

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

谢谢史蒂夫

4

2 回答 2

1

尝试:

s = Shop.new( name: "my shop", city:"NY", state: "NY", user_id: 2)
s.save
于 2013-09-04T00:04:55.687 回答
0

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

于 2013-07-09T18:59:49.160 回答