Question

I am following this rather good rails tutorial as an introduction to the world of ruby on rails.

I have configured my sample app to use postgresql in line with the Heroku site where it is distributed. I can create tables in the database by generating the migration file using rails generate model user name:string email:string and then running bundle rake bd:migrate.

After doing this I can see the tables using pgAdmin3. I then run the rails console in sandbox mode and create some user objects using the active record methods.

user = User.new(name: "Russell Ormes", email: "russell@ormes.com")
=> #<User id: nil, name: "Russell Ormes", email: "russell@ormes.com", created_at: nil, updated_at: nil>
irb(main):003:0> user.save
   (0.3ms)  SAVEPOINT active_record_1
  SQL (93.6ms)  INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Fri, 29 Nov 2013 15:08:30 UTC +00:00], ["email", "russell@ormes.com"], ["name", "Russell Ormes"], ["updated_at", Fri, 29 Nov 2013 15:08:30 UTC +00:00]]
   (0.2ms)  RELEASE SAVEPOINT active_record_1
=> true

and a second user

irb(main):005:0> User.create(name: "A Nother", email: "another@example.org")
   (0.3ms)  SAVEPOINT active_record_1
  SQL (0.7ms)  INSERT INTO "users" ("created_at", "email", "name", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["created_at", Fri, 29 Nov 2013 15:15:23 UTC +00:00], ["email", "another@example.org"], ["name", "A Nother"], ["updated_at", Fri, 29 Nov 2013 15:15:23 UTC +00:00]]
   (0.2ms)  RELEASE SAVEPOINT active_record_1
=> #<User id: 2, name: "A Nother", email: "another@example.org", created_at: "2013-11-29 15:15:23", updated_at: "2013-11-29 15:15:23">

Now I can access these users from the rails console using the User.all

irb(main):010:0> User.all
  User Load (0.6ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, name: "Russell Ormes", email: "russell@ormes.com", created_at: "2013-11-29 15:08:30", updated_at: "2013-11-29 15:08:30">, #<User id: 2, name: "A Nother", email: "another@example.org", created_at: "2013-11-29 15:15:23", updated_at: "2013-11-29 15:15:23">]>

However, when I access the database through pgAdmin3 it shows me no data. (As a new member I don't have enough reputation points to post an image).

I am logging in to pgAdmin3 using the same username as rails (sample) and so feel I should have permission to view any data. Any ideas why I can't see anything? IS this to do with running the console in sandbox mode? I thought it would add the data to the database and them rollback the database on logout.

Thanks for any help :)

Was it helpful?

Solution

When you're running in sanbox mode, you're effectively running a database transaction, which by definition is atomic and isolated. This means that you cannot view the results of the transaction until a COMMIT or ROLLBACK have occured.

This is why you can't see the results in pgAdmin in the same time while you're in the sandbox.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top