# Strange problem with "each"

**URL:** <https://www.ruby-forum.com/t/strange-problem-with-each/193881>\
**Category:** Rails\
**Created:** [August 26, 2010, 3:09am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881 "2010-08-26T03:09:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 26, 2010, 3:09am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881/1 "2010-08-26T03:09:37Z")

</div>

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](http://pastie.org/1116762)

Thanks much.

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 26, 2010, 3:50am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881/2 "2010-08-26T03:50:26Z")

</div>

On Aug 25, 9:09 pm, “Patrick L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 26, 2010, 3:57am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881/3 "2010-08-26T03:57:04Z")

</div>

Tim S. wrote:

> On Aug 25, 9:09ï¿½pm, “Patrick L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
> 
> [ActiveRecord::Associations::ClassMethods](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](http://pastie.org/1116870)

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 26, 2010, 4:50am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881/4 "2010-08-26T04:50:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lynchnco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c77e96/32.png) [@lynchnco](https://www.ruby-forum.com/u/lynchnco)\
**Post date:** [August 26, 2010, 4:51am UTC](https://www.ruby-forum.com/t/strange-problem-with-each/193881/5 "2010-08-26T04:51:35Z")

</div>

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