# Heeelp - no idea what's going wrong

**URL:** <https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304>\
**Category:** Rails\
**Created:** [March 30, 2006, 5:15am UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304 "2006-03-30T05:15:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 5:15am UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/1 "2006-03-30T05:15:22Z")

</div>

I’m working on a simple CMS. The main data type are “listings” a listing  
habtm (has\_and\_belongs\_to\_many) categories and subcategories.  
Subcategories belong\_to categories. Subcategories are basically the same  
as categories, but they are treated differently in a few situations.

The problem is, I can’t seem to write the view/controller/model code  
that allows me to create a listing that contains subcategories! Even  
though I’m using the exact same code I used for categories, the models  
are the same, and the database configs are the same, the MySQL database  
is never updated.

This is the form that’s used:

\<% for category in @categories %\>

 @listing = Listing.new(params[:listing])  
@listing.categories = Category.find(params[:category\_ids] # this  
works  
@listing.subcategories = Subcategory.find(params[:subcategory\_ids] #  
doesn’t  
if @listing.save  
…  
…  
end

And the databases:

create table listings (  
id int not null auto\_increment,  
  
primary key (id)  
);

create table subcategories (  
id int not null auto\_increment  
name varchar(100) not null,  
category\_id int not null,  
constraint fk\_items\_category foreign key (category\_id) references  
categories(id),  
primary key (id)  
);

create table listings\_subcategories  
(practically a duplicate of categories\_listings, which works)  
subcategory\_id int not null,  
listing\_id int not null,  
constraint fk\_cp\_subcategory foreign key (subcategory\_id) references  
subcategories(id),  
constraint fk\_cp\_listing foreign key (listing\_id) references  
listings(id),  
primary key (subcategory\_id, listing\_id)  
);

When I try to create a listing with subcategories the array  
subcategory\_ids is populated correctly, and the listing is created, but  
the listing has no subcategories and the table listings\_subcategories  
has no entries.

This problem has been driving me crazy all day. I’ve tried everything I  
can think of to get it to work. If anyone has a clue, I will be greatly  
indebted.

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 6:31am UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/2 "2006-03-30T06:31:07Z")

</div>

Adam B. wrote:

> \<for subcategory in category.subcategories %\>  
> This is the controller code for create:  
> end  
> id int not null auto\_increment  
> listing\_id int not null,  
> constraint fk\_cp\_subcategory foreign key (subcategory\_id) references  
> subcategories(id),  
> constraint fk\_cp\_listing foreign key (listing\_id) references  
> listings(id),  
> primary key (subcategory\_id, listing\_id)  
> );

You have a composite key for subcategory, but for category you don’t  
have a composite key. Even without this, ActiveRecord will need to be  
told that you’re id for subcategory doesn’t follow the AR conventions  
(id), so you must have set\_primary\_key :subcategory\_id (or something  
like that) in your model for subcategory

Can’t see anything else immediately  
Kev

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 7:23am UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/3 "2006-03-30T07:23:36Z")

</div>

> You have a composite key for subcategory, but for category you don’t  
> have a composite key. Even without this, ActiveRecord will need to be  
> told that you’re id for subcategory doesn’t follow the AR conventions  
> (id), so you must have set\_primary\_key :subcategory\_id (or something  
> like that) in your model for subcategory
> 
> Can’t see anything else immediately  
> Kev

Kev,

Sorry, I’m fairly new with MySQL as well (actually I’m new with all of  
this stuff…) by composite key do you mean the fact that subcategories  
belong\_to categories, i.e. the SQL code:

category\_id int not null,  
constraint fk\_items\_category foreign key (category\_id) references  
categories(id),

whereas categories have no such reference.

Other than that, thanks for the help. I’ll see what I can do with that.

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 8:25am UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/4 "2006-03-30T08:25:59Z")

</div>

> Sorry, I’m fairly new with MySQL as well (actually I’m new with all of  
> this stuff…) by composite key do you mean the fact that subcategories  
> belong\_to categories, i.e. the SQL code:
> 
> category\_id int not null,  
> constraint fk\_items\_category foreign key (category\_id) references  
> categories(id),

create table subcategories (  
…

primary key (id)  
);

create table listings\_subcategories  
subcategory\_id int not null, \<= not id like ActiveRecord expects so  
…  
primary key (subcategory\_id, listing\_id) \<= you have specified a  
composite primary key - ie a key that is composed of two values  
subcategory\_id + listing\_id  
);

see this for a discussion of composite keys and AR support

[http://wrath.rubyonrails.org/pipermail/rails-core/2006-February/000816.html](http://wrath.rubyonrails.org/pipermail/rails-core/2006-February/000816.html)

If you specified  
primary key (subcategory\_id) and it was an auto\_inc style id, then AR  
should have no problems

Kev

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 8:36pm UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/5 "2006-03-30T20:36:56Z")

</div>

Thanks for all the new info. It’s nice to get a more complete  
understanding of how this all works.

However, I’m still not quite sure why this composite key is different  
from the other composite key I’m using:

create table categories (  
id int not null auto\_increment  
name varchar(100) not null,  
primary key (id)  
);

create table categories\_listings  
category\_id int not null,  
listing\_id int not null,  
constraint fk\_cp\_category foreign key (category\_id) references  
categories(id),  
constraint fk\_cp\_listing foreign key (listing\_id) references  
listings(id),  
primary key (category\_id, listing\_id)

Which works fine for categories and listings. I got this setup, as well  
as the form and controller code I posted earlier, from this pdf:

[http://jrhicks.net/Projects/rails/has\_many\_and\_belongs\_to\_many.pdf](http://jrhicks.net/Projects/rails/has_many_and_belongs_to_many.pdf)

Sorry to be so much trouble, and thanks again.

-Adam

---

<div class="post-metadata">

**Author:** ![adambeazley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@adambeazley](https://www.ruby-forum.com/u/adambeazley)\
**Post date:** [March 30, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/heeelp-no-idea-whats-going-wrong/93304/6 "2006-03-30T22:25:00Z")

</div>

I found the problem. I should’ve done a search first; it turns out  
there’s a bug in .save:

[http://www.ruby-forum.com/topic/48785](http://www.ruby-forum.com/topic/48785)

I made the suggested change (add the categories, save, add the  
subcategories, and save again) and it works.

Thanks,

Adam
