Strange problem with "each"

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.

On Aug 25, 9:09 pm, “Patrick L.” [email protected] 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.

Tim S. wrote:

On Aug 25, 9:09�pm, “Patrick L.” [email protected] 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.html

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

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

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

Unbelievable. Thanks David, that did it.

Best,
Patrick

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs