In an attempt to better learn Rails and I’m building a simple Blackjack game, but I’m running into a problem of storing the variable. I understand how to store user-genereated data into the db using form_for, but I’m struggling to create a random number and put it in the db.
@hand = rand(9) + 2
I have “hand” as a field in my db, just curious how to store this random number. Any help anyone could provide to point me in the right direction would be very much appreciated. Thanks.
Here’s some example code:
In this case, I’m assuming that
Gameis a model in your Rails application. Fields (or “database columns”, if you prefer) in thegamestable you can access and set by using their names – so we’re saving ourrandvalue into a field namedhand.(Of course, you have to create that field first – either by having created it via some scaffolding, or by creating a database migration to add that column).
Likewise, to read back the
handvariable.