Problems accessing relational objects

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

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.

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

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.

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

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

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…