Ruby on Rails Tuesday, January 31, 2012

On Jan 31, 7:16 pm, Christopher Jones <li...@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?
>
> Any help would be greatly appreciated.
>
> --
> Posted viahttp://www.ruby-forum.com/

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.

--
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