- A category has parent categories.
- A product is in many categories and a category has many products.
- Products and category both have images in the same image table. ie. a
product and / or category could have multiple images.<=== my question is
related to this
So among other things I presume I have to do the following:
class Category < ActiveRecord:Base
#…
has_and_belongs_to_many products
acts_as_tree :order => “name”
#…
end
class Product < ActiveRecord:Base
#…
has_and_belongs_to_many categories
#…
end
class Image < ActiveRecord:Base
#…
# ??
#…
end
Lets say the image table sql looks like this:
create table images (
image_id int not null,
entity_id int not null,
entity_type varchar(20) not null,
image_path varchar(255) not null,
image_type varchar(20) not null,
primary key(image_id)
);
For example if i were to store the “BIG”, “SMALL” images for a category
in the above table, I would do:
insert into images values(1, 1, “CATEGORY”, “/big/smile1.jpg”, “BIG”);
insert into images values(1, 1, “CATEGORY”, “/small/smile2.jpg”,
“SMALL”);
Also, for example if I were to store the “SMALL”, “MEDIUM” images for a
product in the above same table, I would do:
insert into images values(1, 1, “PRODUCT”, “/big/hifi1.jpg”, “SMALL”);
insert into images values(1, 1, “PRODUCT”, “/small/hifi2.jpg”,
“MEDIUM”);
My question is how do I tell my app using ActiveRecord to map the one to
many association between category and images AND between product and
images in the above scenario where the product and category images map
to the same table? Example code would be a great help.
Thanks.