Ruby on Rails Wednesday, February 1, 2012

On 1 February 2012 01:16, Christopher Jones <lists@ruby-forum.com> wrote:
> 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?

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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment