# Where I'm wrong? (has\_and\_belongs\_to\_many)

**URL:** <https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887>\
**Category:** Rails\
**Created:** [August 22, 2006, 5:45am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887 "2006-08-22T05:45:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2006, 5:45am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/1 "2006-08-22T05:45:06Z")

</div>

in the controller:

@program = Program.find\_by\_id(1)  
@psc = ProgramSpecificCategory.find\_by\_id (2)  
@program.ProgramSpecificCategories \<\< @psc

fails with  
“c:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active\_support/dependencies.rb:100:in  
`const\_missing’: uninitialized constant Programspecificcategory”

Any idea what I’m missing? (completely newbie)

below are the components, if it helps:

class Program \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :programspecificcategories  
end

class ProgramSpecificCategory \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :programs  
end

I also have a common table:

```
create_table :program_specific_categories_programs, :id => false do

```

|t|  
t.column :program\_id, :integer, :limit =\>11  
t.column :programspecificcategory\_id, :integer, :limit =\>11

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2006, 7:07am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/2 "2006-08-22T07:07:13Z")

</div>

has\_and\_belongs\_to\_many :program\_specific\_categories

Mind the under\_scores.

Max

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2006, 7:15am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/3 "2006-08-22T07:15:14Z")

</div>

seems to help a little, but still an error:

"undefined method `ProgramSpecificCategories’ "

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 22, 2006, 8:57am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/4 "2006-08-22T08:57:22Z")

</div>

@program.ProgramSpecificCategories

when you say this it means that there is a method by that name

i.e def ProgramSpecificCategories  
#…  
end

but here thats not the case so what you want to put inside the instance  
variables of @program try to fill in those data i.e the columns of the  
underlying table

what i think you want to do from the code it seems

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 23, 2006, 12:04am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/5 "2006-08-23T00:04:01Z")

</div>

More under\_scores:

@program.ProgramSpecificCategories \<\< @psc

should be

@program.program\_specific\_categories \<\< @psc

and in your migration:

t.column :programspecificcategory\_id, :integer, :limit =\>11

should be

t.column :program\_specific\_category\_id, :integer, :limit =\>11

In Rails, the naming convention is that _only_ class names are  
upeer/camel cased. Everything else (and I mean everything) is  
snake\_cased, including table names, column names, methods names and  
field names.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [August 23, 2006, 1:40am UTC](https://www.ruby-forum.com/t/where-im-wrong-has-and-belongs-to-many/65887/6 "2006-08-23T01:40:12Z")

</div>

YES!

it works, thank you.
