On Aug 25, 9:09 pm, "Patrick L." <li...@ruby-forum.com> 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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to