I do have a belongs_to relationship with users and users has_one game.
The problem isn't with this relationship though. I am able to get
retrieve any information related to the @user, no problem. The problem
I am having is that in my form I have html inputs that need to post to
my database. But the database is looking for integers here and the form
outputs strings.
I need to know if there's an easy (or any) to have these values from my
form be converted to integers before_save. I thought that what I have
below with defining "user_id" as "self.user_id.to_i" would do it but I
get that stack error.
Don’t think you need to define your own method “user_id”, as active record would have already generated one based on DB schema.
Also, conversion between integer and string is too taken care by Active Record. From a web form, all the data are received in string form in the HTTP POST body, so I dont think the issue is with data type conversion.
So I've realized that the model is just not receiving anything from
these fields.
I added:
validates_presence_of :user_id
validates_presence_of :user_empire
validates_presence_of :difficulty
and I get the error in the form:
"User can't be blank
User empire can't be blank"
In the development log it shows these being posted with the correct
names, I've double checked them in the DB and it all matches up. So why
is it not submitting what's in the input to the DB?
Also it says "User can't be Blank" when it should be user_id. I am
completely confused here.
I do have the belongs_to relationship with :users but am not nesting the
new game form. I don't have to still do the accepts_nested_attributes,
do I?