# Navigating model relationships

**URL:** <https://www.ruby-forum.com/t/navigating-model-relationships/79893>\
**Category:** Rails\
**Created:** [December 20, 2006, 1:05pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893 "2006-12-20T13:05:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [December 20, 2006, 1:05pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/1 "2006-12-20T13:05:02Z")

</div>

i’m trying to understand navigating activerecord relationships. For  
example I’ve got:

class Product \< ActiveRecord::Base  
has\_many :line\_items  
end  
class LineItem \< ActiveRecord::Base  
belongs\_to :product  
has\_one :price  
end  
class Price \< ActiveRecord::Base  
belongs\_to :line\_item  
end

ok so Price isn’t the best example but the point is LineItem has\_one  
Price.  
so I’ve passed a Product object from the controller into a view and i’m  
iterating over it like thus:

\<% for lineitem in @product.line\_items %\>

then I want to write a conditional statement to test if there’s a price  
object for that lineitem so:

\<% if lineitem.price %\>  
print something  
\<% end %\>

the problem is the “if” line raises:

(eval):1:in `compute\_type’: compile error  
(eval):1: parse error, unexpected tINTEGER  
Object::1

so what am I doing wrong?

thanks

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [December 20, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/2 "2006-12-20T14:24:04Z")

</div>

creativetags wrote:

> i’m trying to understand navigating activerecord relationships. For  
> example I’ve got:
> 
> class Product \< ActiveRecord::Base  
> has\_many :line\_items  
> end  
> class LineItem \< ActiveRecord::Base  
> belongs\_to :product  
> has\_one :price  
> end  
> class Price \< ActiveRecord::Base  
> belongs\_to :line\_item  
> end
> 
> ok so Price isn’t the best example but the point is LineItem has\_one  
> Price.  
> so I’ve passed a Product object from the controller into a view and i’m  
> iterating over it like thus:
> 
> \<% for lineitem in @product.line\_items %\>
> 
> then I want to write a conditional statement to test if there’s a price  
> object for that lineitem so:
> 
> \<% if lineitem.price %\>  
> print something  
> \<% end %\>
> 
> the problem is the “if” line raises:
> 
> (eval):1:in `compute\_type’: compile error  
> (eval):1: parse error, unexpected tINTEGER  
> Object::1
> 
> so what am I doing wrong?
> 
> thanks

I suspect it’s because ‘price’ isn’t a boolean. Perhaps

unles lineitem.price.nil?

unless lineitem.price.blank?

Alan

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [December 20, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/3 "2006-12-20T18:29:45Z")

</div>

Hi –

On Wed, 20 Dec 2006, Alan F. wrote:

> > has\_one :price  
> > \<% for lineitem in @product.line\_items %\>  
> > (eval):1:in `compute\_type’: compile error
> 
> unless lineitem.price.blank?

That shouldn’t matter; every Ruby expression has a boolean value of  
true or false, for purposes of conditional evaluation.

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [December 20, 2006, 6:44pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/4 "2006-12-20T18:44:01Z")

</div>

Hi –

On Wed, 20 Dec 2006, creativetags wrote:

> end
> 
> (eval):1: parse error, unexpected tINTEGER  
> Object::1
> 
> so what am I doing wrong?

Do you have an \<% end %\> for the “for” loop as well as the “if”?

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [April 30, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/5 "2007-04-30T17:55:21Z")

</div>

It seems that “type” is a forbidden column name in Ruby’s ActiveRecord.

More details here:  
[http://nhw.pl/wp/2006/05/26/column-names-restrictions/](http://nhw.pl/wp/2006/05/26/column-names-restrictions/)

Hope that helps.

–Stephen

---

<div class="post-metadata">

**Author:** ![creativetags](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/9f8e36/32.png) [@creativetags](https://www.ruby-forum.com/u/creativetags)\
**Post date:** [December 20, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/navigating-model-relationships/79893/6 "2006-12-20T20:01:21Z")

</div>

> I suspect it’s because ‘price’ isn’t a boolean. Perhaps
> 
> unless lineitem.price.nil?
> 
> unless lineitem.price.blank?

sorry still giving the same error.

mark
