Multiple feature creation

In my example, I only saw the wishlist by asking for it. I was testing
in console, and I called User.create! with parameters, and when that
saved, I did not see anything about the wishlist being created in the
console output.

In the next line I used a trick – underscore picks up the return value
from the previous line in Rails console. So I called _.wishlist to find
out if the new User had a wishlist. What do you see when you ask for
Wishlist.all in console?

I’ll try running it again in rails server and see if the server log
indicates the wishlist is being created.

Apparently so:

Started GET “/users/sign_up” for 127.0.0.1 at Fri Feb 10 15:20:48 -0500
2012
Processing by Devise::RegistrationsController#new as HTML
Rendered
/Users/waltd/.rvm/gems/ruby-1.8.7-p352/gems/devise-1.5.3/app/views/devise/shared/_links.erb
(1.4ms)
Rendered
/Users/waltd/.rvm/gems/ruby-1.8.7-p352/gems/devise-1.5.3/app/views/devise/registrations/new.html.erb
within layouts/application (20.1ms)
Completed 200 OK in 47ms (Views: 44.8ms | ActiveRecord: 0.0ms)

Started POST “/users” for 127.0.0.1 at Fri Feb 10 15:21:02 -0500 2012
Processing by Devise::RegistrationsController#create as HTML
Parameters: {“commit”=>“Sign up”,
“authenticity_token”=>“IezLEQXCXqvMaJRaIIURbzNJWp5CU6YPx8/prrrrS9Q=”,
“utf8”=>“✓”, “user”=>{“password_confirmation”=>“[FILTERED]”,
“password”=>“[FILTERED]”, “email”=>“[email protected]”}}
SQL (0.2ms) SELECT 1 FROM “users” WHERE (“users”.“email” =
[email protected]’) LIMIT 1
SQL (0.1ms) SELECT name
FROM sqlite_master
WHERE type = ‘table’ AND NOT name = ‘sqlite_sequence’
AREL (0.4ms) INSERT INTO “wishlists” (“updated_at”, “user_id”,
“created_at”) VALUES (‘2012-02-10 20:21:02.788524’, NULL, ‘2012-02-10
20:21:02.788524’)
AREL (0.2ms) INSERT INTO “users” (“current_sign_in_at”,
“last_sign_in_at”, “last_sign_in_ip”, “encrypted_password”, “email”,
“sign_in_count”, “current_sign_in_ip”, “remember_created_at”, “name”,
“reset_password_sent_at”, “reset_password_token”, “updated_at”,
“created_at”) VALUES (NULL, NULL, NULL,
‘$2a$10$v6bk7ld4W5MG4F0H91vpregCN/xF0P.gwyM9tfH4tQ/H8zAMU3i4a’,
[email protected]’, 0, NULL, NULL, NULL, NULL, NULL, ‘2012-02-10
20:21:02.822993’, ‘2012-02-10 20:21:02.822993’)
AREL (0.1ms) UPDATE “wishlists” SET “updated_at” = ‘2012-02-10
20:21:02.825246’, “user_id” = 6 WHERE “wishlists”.“id” = 5
AREL (0.2ms) UPDATE “users” SET “current_sign_in_at” = ‘2012-02-10
20:21:02.827881’, “last_sign_in_at” = ‘2012-02-10 20:21:02.827881’,
“last_sign_in_ip” = ‘127.0.0.1’, “sign_in_count” = 1,
“current_sign_in_ip” = ‘127.0.0.1’, “updated_at” = ‘2012-02-10
20:21:02.828227’ WHERE “users”.“id” = 6
Redirected to http://localhost:3000/
Completed 302 Found in 268ms

You’re going to have to show me your models if I’m going to be of any
further help to you here.

Walter

On Feb 10, 2012, at 3:57 PM, Christopher J. wrote:

contain between 8 to 20 characters"
def encrypt_password
BCrypt::Engine.hash_secret(password, user.password_salt)
class Wishlist < ActiveRecord::Base
belongs_to :user
validates_presence_of :number_1
validates_presence_of :number_2
validates_presence_of :number_3
validates_presence_of :number_4
validates_presence_of :number_5

This is your problem right here. Your validations aren’t passing because
nothing is being added to the wishlist in my code. Do you have these
values (number_1 etc) at the moment that the user is being created? If
so, where do they live at that precise moment?

Walter

Walter D. wrote in post #1045281:

On Feb 10, 2012, at 3:57 PM, Christopher J. wrote:

contain between 8 to 20 characters"
def encrypt_password
BCrypt::Engine.hash_secret(password, user.password_salt)
class Wishlist < ActiveRecord::Base
belongs_to :user
validates_presence_of :number_1
validates_presence_of :number_2
validates_presence_of :number_3
validates_presence_of :number_4
validates_presence_of :number_5

This is your problem right here. Your validations aren’t passing because
nothing is being added to the wishlist in my code. Do you have these
values (number_1 etc) at the moment that the user is being created? If
so, where do they live at that precise moment?

Walter

These are in the wishlist table of the database, How would I go about
making them present?

That makes total sense, how would it be able to be created if it
demanded presence. I tried it without the mandatory validation and it
worked.

Thanks for all the help Walter, appreciate it a lot.

On Feb 10, 2012, at 4:55 PM, Christopher J. wrote:

validates_presence_of :number_2

These are in the wishlist table of the database, How would I go about
making them present?

No, what I meant was, has the visitor typed them into a form field on
the page? Where is the desired value coming from? Are these values
present in the params hash when the form is being submitted?

If they’re not there yet, and if you want the User to be created with or
without a fully-populated wish list, then remove these validations and
your wishlist will save as it did in my example.

Also, Colin is right, and I had written nearly the same thing but erased
it because I wanted to solve one problem at a time. You really ought to
be saving the individual wishlist_items as separate objects. Make
another model called WishlistItem with belongs_to :wishlist, set
Wishlist to has_many :wishlist_items and then you can make a nested form
that will save one or many items to a wish list.

Walter