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

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

your logic seems right as the only things that will return false are the boolean false and nil.

You may want to check to make sure that you have the correct columns in your database. the table prices should have line_item_id and line_items should contain product_id . But even that probably wouldn’t return the error that you’re receiving.

Other than that all I can think of is to check the rest of your code leading to that line.

ok i just checked all the code - the db seems to be setup correctly with foreign keys.

I tested the original 'if' statement i proposed and the condition returns false (or nil) skipping the block if there is no price object associated with the lineitem in the db. I still get the error about unexpected tINTEGER if there is an associated price.

Anyone know what that means? what it should be?

thanks

Hi --

Hi --

Do you have an <% end %> for the "for" loop as well as the "if"?

yeah sorry didn't include it up there for speed. thanks anyway.

did you set up the database using migrations or sql statements? it almost seems like a type mismatch: it’s getting an integer when it’s expecting a float for example.

i set up the db with migrations. i can't see any wrong types in the db schema that I know of.

It's strange because it's not actually returning an integer to lineitem.price it's throwing an error saying it's returning an integer. Is it saying that because it's expecting a boolean for the 'if' condition? If so how do I write a condition to test if a lineitem has a price?

From what you described, the error is a compile error and has nothing

to do with object type at runtime. I would double check your source code to make sure you don't have an integer on that line somewhere (like a 1 for the letter i). If your still having a problem post the exact source code and someone will be able to figure it out.

-Paul

i think i'm getting somewhere now - 'type' appears to be reserved for single table inheritance. the things you don't think of when you're a newbie :slight_smile:

thanks again for all your help