# Problems with belongs\_to table joins

**URL:** <https://www.ruby-forum.com/t/problems-with-belongs-to-table-joins/61055>\
**Category:** Rails\
**Created:** [May 19, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/problems-with-belongs-to-table-joins/61055 "2006-05-19T18:30:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robl](https://www.ruby-forum.com/u/robl)\
**Post date:** [May 19, 2006, 6:30pm UTC](https://www.ruby-forum.com/t/problems-with-belongs-to-table-joins/61055/1 "2006-05-19T18:30:09Z")

</div>

I am looking for a little assistance with a problem I have with a simple  
table join using Rails. I am a relative newbie to both Ruby and Rails so  
any help would be greatly appreciated . I have two tables products and  
product\_formats.

create table products (  
id int not null auto\_increment,  
title varchar(100) not null,  
product\_format\_id integer references product\_formats(id),  
description text not null,  
image\_url varchar(200) not null,  
products\_quantity int(11) not null,  
products\_status enum(‘0’,‘1’) not null,  
available\_from date,  
cost\_price decimal(10,2) not null,  
retail\_price decimal(10,2) not null,  
primary key(id)  
);

class Product \< ActiveRecord::Base

validates\_presence\_of :title  
validates\_numericality\_of :retail\_price, :cost\_price

has\_and\_belongs\_to\_many :artists  
has\_and\_belongs\_to\_many :labels  
has\_and\_belongs\_to\_many :genres

belongs\_to :product\_formats

end

create table product\_formats (  
id int not null auto\_increment,  
format varchar(30),  
primary key(id)  
);

class ProductFormat \< ActiveRecord::Base  
end

I have successfully created many to many table joins and they are  
working fine. But alas I can quite stumped on how to get a one to many  
join working ok.

I would just like to list out the products and (where the type is  
specified their type)

def testlist  
@products = Product.find(:all)  
end

As far as I know the above will select all the products from the table  
and the belongs\_to method will automagically create the method

@products.product\_types

so in theory I should be able to list product information like so

\<%=product.title%\> \<%=product.product\_format\_id%\>  
\<%=product.product\_format.format%\>

However, I keep getting the following error, which is presumably  
indicating that the join hasn’t occured. Indeed looking in the  
development.log I can’t see any join or select from the table  
product\_formats.

undefined method `product\_format’ for #Product:0xb789c580

Can anyone steer me in the right direction?

---

<div class="post-metadata">

**Author:** ![robl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robl](https://www.ruby-forum.com/u/robl)\
**Post date:** [May 19, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/problems-with-belongs-to-table-joins/61055/2 "2006-05-19T18:48:27Z")

</div>

class ProductFormat \< ActiveRecord::Base  
has\_many :products  
end

---

<div class="post-metadata">

**Author:** ![robl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9a140/32.png) [@robl](https://www.ruby-forum.com/u/robl)\
**Post date:** [May 19, 2006, 8:24pm UTC](https://www.ruby-forum.com/t/problems-with-belongs-to-table-joins/61055/3 "2006-05-19T20:24:05Z")

</div>

Michael T. wrote:

> class ProductFormat \< ActiveRecord::Base  
> has\_many :products  
> end

Thanks, but to be honest I had tried that as well, but as far as I can  
tell from other working applications this isn’t always necessary.  
Either way, I have added this my product\_format.rb model and its still  
giving the same error :S
