Hello all, This is my first time setting up associations in Rails. A User :has_many Jobs, and a Job :belongs_to a User. Take a peak at this code and tell me what you think. For some reason, when I pull these jobs and try to iterate through to show a list of Jobs posted by a User, I get a strange error with the "each" function. http://pastie.org/1116762 Thanks much. -- 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 Aug 25, 9:09 pm, "Patrick L." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello all, > This is my first time setting up associations in Rails. A User :has_many > Jobs, and a Job :belongs_to a User. > > Take a peak at this code and tell me what you think. For some reason, > when I pull these jobs and try to iterate through to show a list of Jobs > posted by a User, I get a strange error with the "each" function. > > http://pastie.org/1116762 > > Thanks much.This is part of your problem: @user.jobs.find_by_user_id(@user.id, :all) Since you are accessing jobs through the @user instance, you don''t need to specify the "find by user id". Rails already knows to only find jobs associated with that user. You can just do this: @user.jobs And this: @user.jobs each do | job | Make sure you have "has_many :jobs" in your User model. Didn''t see that posted in your code. Also, might not be a bad idea to familiarize yourself with the Rails Association documentation. It will answer a lot of questions like this. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html -- 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.
Tim Shaffer wrote:> On Aug 25, 9:09�pm, "Patrick L." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Thanks much. > This is part of your problem: > > @user.jobs.find_by_user_id(@user.id, :all) > > Since you are accessing jobs through the @user instance, you don''t > need to specify the "find by user id". Rails already knows to only > find jobs associated with that user. > > You can just do this: @user.jobs > > And this: @user.jobs each do | job | > > Make sure you have "has_many :jobs" in your User model. Didn''t see > that posted in your code. > > Also, might not be a bad idea to familiarize yourself with the Rails > Association documentation. It will answer a lot of questions like > this. > > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.htmlHi Tim, I implemented your changes. Unfortunately, I still got the same error. I copied both the User and Jobs models, as well as the schema.rb file. Please let me know your thoughts. http://pastie.org/1116870 -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Hi Tim, > I implemented your changes. Unfortunately, I still got the same error. I > copied both the User and Jobs models, as well as the schema.rb file. > > Please let me know your thoughts. > > http://pastie.org/1116870You have a minor typo in index.html.erb <% unless @user.jobs == nil %> <% @user.jobs each do |job| %> <%= job.Title %> <% end %> <% end %> You want `@user.jobs.each` not `@user.jobs each`. Note the missing dot between "jobs" and "each". -- med vänlig hälsning David J. Hamilton -- 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.
David J. Hamilton wrote:>> Hi Tim, >> I implemented your changes. Unfortunately, I still got the same error. I >> copied both the User and Jobs models, as well as the schema.rb file. >> >> Please let me know your thoughts. >> >> http://pastie.org/1116870 > > You have a minor typo in index.html.erb > > <% unless @user.jobs == nil %> > <% @user.jobs each do |job| %> > <%= job.Title %> > <% end %> > <% end %> > > You want `@user.jobs.each` not `@user.jobs each`. Note the missing > dot between "jobs" and "each". > > -- > med v�nlig h�lsning > David J. HamiltonUnbelievable. Thanks David, that did it. Best, Patrick -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.