# Problem with has\_many :through association

**URL:** <https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167>\
**Category:** Rails\
**Created:** [October 3, 2007, 3:51am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167 "2007-10-03T03:51:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 3, 2007, 3:51am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/1 "2007-10-03T03:51:17Z")

</div>

I have the following models

1. class Caregiver \< ActiveRecord::Base
2. has\_many :call\_orders
3. has\_many :users, :through =\> :call\_orders
4. end
5. 
6. class User \< ActiveRecord::Base
7. has\_many :call\_orders, :order =\> :position
8. has\_many :caregivers, :through =\> :call\_orders
9. end
10. 
11. class CallOrder \< ActiveRecord::Base
12. belongs\_to :user
13. belongs\_to :caregiver
14. acts\_as\_list :scope =\> :user
15. end

When I run the following commands, I get an error:

1. @call\_list = User.find(params[:id])
2. @call\_list.call\_orders.each do |call\_order|
3. puts call\_order.caregiver.first\_name

This is the error:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.first\_name

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 7:04am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/2 "2007-10-04T07:04:18Z")

</div>

chirag wrote:

> 1. end
> 2. @call\_list = User.find(params[:id])
> 3. @call\_list.call\_orders.each do |call\_order|
> 4. puts call\_order.caregiver.first\_name
> 
> This is the error:
> 
> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.first\_name

What do your tables/migrations look like?

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 7:13am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/3 "2007-10-04T07:13:39Z")

</div>

class CreateCaregivers \< ActiveRecord::Migration  
def self.up  
create\_table :caregivers do |t|  
t.column :first\_name, :string  
t.column :last\_name, :string  
t.column :address, :string  
t.column :city, :string  
t.column :state, :string  
t.column :home\_phone, :string  
t.column :work\_phone, :string  
t.column :cell\_phone, :string  
t.column :relationship, :string  
t.column :email, :string  
end  
end

def self.down  
drop\_table :caregivers  
end  
end

class CreateCallOrders \< ActiveRecord::Migration  
def self.up  
create\_table :call\_orders do |t|  
t.column :user\_id, :integer  
t.column :caregiver\_id, :integer  
t.column :position, :integer  
end  
end

def self.down  
drop\_table :call\_orders  
end  
end

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table “users”, :force =\> true do |t|  
t.column :login, :string  
t.column :email, :string  
t.column :crypted\_password, :string, :limit =\> 40  
t.column :salt, :string, :limit =\> 40  
t.column :created\_at, :datetime  
t.column :updated\_at, :datetime  
t.column :remember\_token, :string  
t.column :remember\_token\_expires\_at, :datetime

```
  t.column :activation_code, :string, :limit => 40
  t.column :activated_at, :datetime
end

```

end

def self.down  
drop\_table “users”  
end  
end

Thanks for the help!  
Chirag

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 7:33am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/4 "2007-10-04T07:33:31Z")

</div>

Migrations look fine and it’s working for me:

> > call\_list = User.find(1)  
> > =\> #\<User:0x3447480 @attributes={“salt”=\>nil, “activated\_at”=\>nil,  
> > “updated\_at”=\>“2007-10-03 22:25:01”, “crypted\_password”=\>nil,  
> > “activation\_code”=\>nil, “remember\_token\_expires\_at”=\>nil, “id”=\>“1”,  
> > “remember\_token”=\>nil, “login”=\>“test”, “created\_at”=\>“2007-10-03  
> > 22:25:01”, “email”=\>nil}\>
> 
> > call\_list.call\_orders.each do |call\_order|  
> > ?\> puts call\_order.caregiver.first\_name
> 
> > end  
> > John

> > call\_list.call\_orders[0].caregiver  
> > =\> #\<Caregiver:0x3441634 @attributes={“work\_phone”=\>nil, “city”=\>nil,  
> > “id”=\>“1”, “relationship”=\>nil, “cell\_phone”=\>nil, “home\_phone”=\>nil,  
> > “first\_name”=\>“John”, “address”=\>nil, “last\_name”=\>“Smith”,  
> > “email”=\>nil, “state”=\>nil}\>

My guess is you have some bad data somewhere.

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 10:29am UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/5 "2007-10-04T10:29:00Z")

</div>

Im agree with Michael.

Btw, you can improve your code with a nested :include option like  
this:  
@call\_list = User.find(params[:id], :include =\> [ { :call\_orders  
=\> :caregiver } ])

Take a look at this: [http://snippets.dzone.com/posts/show/2089](http://snippets.dzone.com/posts/show/2089) for a  
better  
explanation.

Regards.  
Pablo C…

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/6 "2007-10-04T13:44:12Z")

</div>

On 3 Oct 2007, at 02:50, chirag wrote:

> You have a nil object when you didn’t expect it!  
> The error occurred while evaluating nil.first\_name

Is that just the way the data is? caregiver is belongs\_to of  
callorder: call\_order.caregiver can be nil (unless you’ve got other  
contraints that forbid it)

Fred

---

<div class="post-metadata">

**Author:** ![chirag](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ecccb3/32.png) [@chirag](https://www.ruby-forum.com/u/chirag)\
**Post date:** [October 4, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/problem-with-has-many-through-association/116167/7 "2007-10-04T19:10:25Z")

</div>

> Is that just the way the data is? caregiver is belongs\_to of  
> callorder: call\_order.caregiver can be nil (unless you’ve got other  
> contraints that forbid it)

Freederick mades a good point here. If nil values are accepted as your  
model definition suggests, then you should use a “compacted” list of  
caregivers like:

@call\_list.call\_orders.collect(&:caregiver).compact.each do |  
caregiver|  
puts caregiver.first\_name  
end

if nil values are not accepted you should add valitadates\_presence\_of  
and validates\_associated to force a caregiver to be present:

class CallOrder \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :caregiver  
acts\_as\_list :scope =\> :user

validates\_presence\_of :caregiver\_id  
validates\_associated :caregiver  
end

Regards  
Pablo C…
