User and Game Association help.

Hi all, I am currently doing work on my rails project and have hit a
wall. I am quite new to Rails so at the moment only know a little bit
about it.

I have a users table and a games table. At the moment I am able to enter
in user information as well as add new games to the games table.

What I wish to do now is to combine the both using a foreign key. I have
added the user_id entry to the games table and even declared it as a
foreign key by writing it as a command through MySQL. I have also
declared them as followed:

model Games < ActiveRecord::Base
  belongs_to :user
end
model User < ActiveRecord::Base
  has_many :games
end

Is there anything else I must do. I want to be able to on the user
profile show all games associated with that particular user. How would I
go about doing this?

Any help would be greatly appreciated.

For what you describe, that should work. As a note, games can have
only one user. If that was not your intention, look at has_many
through and has_and_belongs_to_many.

If you have a user, current_user for example, then his games will be
current_user.games. I think maybe you need to work through some
tutorials to get the basics of rails. railstutorial.org is good and
is free to use online. Work right through that (entering and testing
the code not just reading it) and you will get a good grasp of what
rails is all about. Make sure you install the correct version of
rails for the tutorial.
For more infomation on associations look at the Rails Guide on
ActiveRecord associations (also the other guides).

Colin

If you have a user, current_user for example, then his games will be
current_user.games.

At the moment I have under the profile @user.games that should return
all games associated with that particular user but no results.

I would like it so that when a new game is added it will associate
itself with the current logged in user rather than entering the
username. Would I have to do this as part of the _form.html.erb input
for games or must this be declared in a model?

All help will be greatly appreciated.

If you have a user, current_user for example, then his games will be
current_user.games.

At the moment I have under the profile @user.games that should return
all games associated with that particular user but no results.

In that case the user has no games.

I would like it so that when a new game is added it will associate
itself with the current logged in user rather than entering the
username. Would I have to do this as part of the _form.html.erb input
for games or must this be declared in a model?

Assuming the current user is available as current_user then
current_user.games will return all his games.

As I said if you work through some tutorials and the guides you will
save yourself a lot of time in the long run. Also after doing this
you will look back here at the questions you asked and be embarrassed
:slight_smile:

Also read up about using the rails console and have a look at the
rails guide on debugging.

Colin

Ok thanks.

Do you have any tutorials to recommend it regards to my problem?

That would be great help.

Thanks.

Hey Colin, I fixed it :slight_smile:

Stupid me, I simply added the following line to my create method in the
controller.

@game.user = current_user

Now I just need to figure out a way of displaying the actual games under
the profiles so that other users can view.

One wall down, another wall to go.

As I suggested in my previous post railstutorial.org is good and free
to use online. Work right through it (don't just read it).

Colin

Hey Colin, I fixed it :slight_smile:

Stupid me, I simply added the following line to my create method in the
controller.

@game.user = current_user

I would think carefully about what you think that line is doing. Is
the game currently associated with a user. If so then which user is
it associated with after that line?

This is definitely one of those situations where if you come back and
look at this in three months time you will be embarrassed. Get on
with those tutorials before doing anything else.

Colin

...

Stupid me, I simply added the following line to my create method in the
controller.

@game.user = current_user

I would think carefully about what you think that line is doing. Is
the game currently associated with a user.

Seeing as it's the create method, probably not, unless he's taken
specific steps to associate it previously in new or create....

-Dave

Good point, that is what comes from not reading the post carefully.
Now it is me that is embarrassed :slight_smile:
Apologies to Christopher for casting aspersions on his code.

Colin

Apologies to Christopher for casting aspersions on his code.

That's fine Colin, we all make mistakes, myself in particular :slight_smile:

It is all functioning well at the moment, just need to make a few
validation checks etc.

The tutorials are great also, done many of them through the book Agile
Web Development with Rails fourth Edition.