# Problems accessing relational objects

**URL:** <https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863>\
**Category:** Rails\
**Created:** [December 31, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863 "2006-12-31T14:06:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [December 31, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/1 "2006-12-31T14:06:51Z")

</div>

Hi. I am frustrated here. I cannot access the attributes of the  
relational model. This should be straight forward and simple, I guess  
that’s why it’s frustrating. Let’s start with the models:

##### Models

class Ad \< ActiveRecord:Base  
belongs\_to :book

class Book \< Inventory  
has\_one :ad

(i’ve also played with has\_many, but same scenario)

##### Ads Controller

@ads = Ad.find\_all\_by\_user\_id session[:user] #grab all ads from current  
user

##### View

\<% @ads.each do |ad| %\>  
\<%= ad.book.nil? %\> (will say it’s false, but yet…)  
\<%= ad.book.attributes %\> (will give the following error:  
\<% end %\>

“You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occured while evaluating nil.attributes”

Can someone help?

And happy New Years Eve Everyone!

D

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [December 31, 2006, 6:45pm UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/2 "2006-12-31T18:45:03Z")

</div>

Dominic S. wrote:

> \<% @ads.each do |ad| %\>  
> \<%= ad.book.nil? %\> (will say it’s false, but yet…)  
> \<%= ad.book.attributes %\> (will give the following error:  
> \<% end %\>
> 
> “You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occured while evaluating nil.attributes”

I would try looking at the objects with debug:  
\<%= debug ad %\>  
\<%= debug ad.book %\>

Dan M.

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [December 31, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/3 "2006-12-31T23:24:18Z")

</div>

Yes, did that and all the objects are in fact in the @ads variable, yet  
I cannot access them… ☹

Dan M. wrote:

> Dominic S. wrote:
> 
> > \<% @ads.each do |ad| %\>  
> > \<%= ad.book.nil? %\> (will say it’s false, but yet…)  
> > \<%= ad.book.attributes %\> (will give the following error:  
> > \<% end %\>
> > 
> > “You have a nil object when you didn’t expect it!  
> > You might have expected an instance of ActiveRecord::Base.  
> > The error occured while evaluating nil.attributes”
> 
> I would try looking at the objects with debug:  
> \<%= debug ad %\>  
> \<%= debug ad.book %\>
> 
> Dan M.

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [January 1, 2007, 2:52am UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/4 "2007-01-01T02:52:41Z")

</div>

Man, if this is the case, STI needs to be a lot more flexible. I couldnt  
use STI with an acts\_as\_tree also : (  
(why would I do that? for a more human readable code to access the  
different node levels…)

i do have a hint that this is the case, and i will try taking out the  
STI…

Thanks Joe and Dan

Joe Hartman wrote:

> > class Book \< Inventory  
> > has\_one :ad
> 
> Dominic
> 
> Looks like you’re using STI? I’ve never done this with STI. No reason to  
> think it wouldn’t work though. You might start with a simpler example  
> not using STI - as in the Agile Web Dev book p. 231.
> 
> Also, try using script/console to test things. Very handy tool for  
> sussing this stuff out.
> 
> The table structure, model, and code all need to be in sync.
> 
> Good luck
> 
> Joe

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [January 1, 2007, 1:47am UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/5 "2007-01-01T01:47:39Z")

</div>

> class Book \< Inventory  
> has\_one :ad

Dominic

Looks like you’re using STI? I’ve never done this with STI. No reason to  
think it wouldn’t work though. You might start with a simpler example  
not using STI - as in the Agile Web Dev book p. 231.

Also, try using script/console to test things. Very handy tool for  
sussing this stuff out.

The table structure, model, and code all need to be in sync.

Good luck

Joe

---

<div class="post-metadata">

**Author:** ![3nd3r](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/3/c4cdca/32.png) [@3nd3r](https://www.ruby-forum.com/u/3nd3r)\
**Post date:** [January 2, 2007, 6:04am UTC](https://www.ruby-forum.com/t/problems-accessing-relational-objects/80863/6 "2007-01-02T06:04:20Z")

</div>

My bad, STI does work. I had deleted inventory that @ads was trying to  
call, that’s why it returned nil. It’s not nil on all returned entries,  
just a few…
