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?