Navigating model relationships

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

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

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)
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)

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)
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)

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/

Hope that helps.

–Stephen

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