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 via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 31, 7:16 pm, Christopher Jones <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 1 February 2012 01:16, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
> 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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 February 2012 11:39, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:>> 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 :) Also read up about using the rails console and have a look at the rails guide on debugging. Colin> > All help will be greatly appreciated. > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ok thanks. Do you have any tutorials to recommend it regards to my problem? That would be great help. Thanks. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hey Colin, I fixed it :) 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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 February 2012 12:32, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Ok thanks. > > Do you have any tutorials to recommend it regards to my problem?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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 February 2012 12:41, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey Colin, I fixed it :) > > Stupid me, I simply added the following line to my create method in the > controller. > > @game.user = current_userI 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> > 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- gplus.to/clanlaw -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Wed, Feb 1, 2012 at 08:21, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 1 February 2012 12:41, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:...>> 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 -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 1 February 2012 16:04, Dave Aronson <googlegroups2dave-BRiZGj7G2yRXqviUI+FSNg@public.gmane.org> wrote:> On Wed, Feb 1, 2012 at 08:21, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > >> On 1 February 2012 12:41, Christopher Jones <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: > ... >>> 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....Good point, that is what comes from not reading the post carefully. Now it is me that is embarrassed :) Apologies to Christopher for casting aspersions on his code. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
> Apologies to Christopher for casting aspersions on his code.That''s fine Colin, we all make mistakes, myself in particular :) 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. -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.