Rails Console Sandbox : .save | .create

76 views
Skip to first unread message

Francesco De Grandi

unread,
May 24, 2012, 9:58:14 AM5/24/12
to Ruby on Rails: Talk
At the end of Rails Tutorial chapter 6, I have the
development .sqlite3 db : 1 table and 1 row (= user id:1 see below) :
> when running from the Rails Concole Sandbox, to create other rows/records, I can not save or create
> here is what I get - Rollback - without even exiting the Rail Console ! ! !
> it is like if an exception is raised somehow

1.9.3p125 :002 > user1 = User.first

User Load (0.2ms) SELECT "users".* FROM "users" LIMIT 1
=> #<User id: 1, name: "Michael Hartl", email: "mha...@example.com",
created_at: "2012-05-22 10:32:16", updated_at: "2012-05-22 10:32:16",
password_digest: "$2a
$10$Eh2xj8CvvKaDFD2uel4LbOQ2dMsCmENy8tyts1BsFzJb...">

1.9.3p125 :002 > user2 = User.create(name: "Francois DG", email:
"f...@example.com")

(0.1ms) SAVEPOINT active_record_1
User Exists (0.2ms) SELECT 1 FROM "users" WHERE
LOWER("users"."email") = LOWER('f...@example.com') LIMIT 1
(0.1ms) ROLLBACK TO SAVEPOINT active_record_1
=> #<User id: nil, name: "Francois DG", email: "f...@example.com",
created_at: nil, updated_at: nil, password_digest: nil>

CAN SOMEBODY TELL ME WHY & WHAT TO DO ?

Frederick Cheung

unread,
May 25, 2012, 4:16:59 AM5/25/12
to Ruby on Rails: Talk


On May 24, 2:58 pm, Francesco De Grandi <fdgar...@gmail.com> wrote:
> At the end of Rails Tutorial chapter 6, I have the
> development .sqlite3 db : 1 table and 1 row (= user id:1 see below) :
>
> > when running from the Rails Concole Sandbox, to create other rows/records, I can not save or create
> > here is what I get - Rollback - without even exiting the Rail Console ! ! !
> > it is like if an exception is raised somehow
>

Sounds like you've got a failing validation - user2.errors will tell
you which validations (if any) have failed.

Fred
> 1.9.3p125 :002 > user1 = User.first
>
> User Load (0.2ms)  SELECT "users".* FROM "users" LIMIT 1
>  => #<User id: 1, name: "Michael Hartl", email: "mha...@example.com",
> created_at: "2012-05-22 10:32:16", updated_at: "2012-05-22 10:32:16",
> password_digest: "$2a
> $10$Eh2xj8CvvKaDFD2uel4LbOQ2dMsCmENy8tyts1BsFzJb...">
>
> 1.9.3p125 :002 > user2 = User.create(name: "Francois DG", email:
> "f...@example.com")
>
>   (0.1ms)  SAVEPOINT active_record_1
>   User Exists (0.2ms)  SELECT 1 FROM "users" WHERE
> LOWER("users"."email") = LOWER('...@example.com') LIMIT 1

Francesco DG

unread,
May 25, 2012, 7:56:27 AM5/25/12
to rubyonra...@googlegroups.com
Thank you Fred !

Meanwhile I simply realized that I had to submit a new user with all attributes and not just 2 :
> that was why the validation did not work and the transaction was rolled back.

Thanks for the user2.errors hint...

Francesco
Reply all
Reply to author
Forward
0 new messages