# Problem with foreign keys not working

**URL:** <https://www.ruby-forum.com/t/problem-with-foreign-keys-not-working/129462>\
**Category:** Rails\
**Created:** [February 18, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/problem-with-foreign-keys-not-working/129462 "2008-02-18T21:14:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Backov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Backov](https://www.ruby-forum.com/u/Backov)\
**Post date:** [February 18, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/problem-with-foreign-keys-not-working/129462/1 "2008-02-18T21:14:02Z")

</div>

I’m a rails noob. 🙂

That said, I’ve been doing not too bad but am stumbling over a basic  
problem, probably a naming issue. Here’s the steup:

class GeneralModType \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :GeneralMods

end

class GeneralMod \< ActiveRecord::Base  
belongs\_to :GeneralModType  
end

class CreateGeneralMods \< ActiveRecord::Migration  
def self.up  
create\_table :general\_mods do |t|  
t.string :name

t.integer :general\_mod\_type\_id, :rangebonus, :damagebonus,  
:srdamagebonus, :reloadbonus, :accbonus

```
  t.timestamps
end

```

end

def self.down  
drop\_table :general\_mods  
end  
end

Now, from all the research I am frantically doing, this seems like it  
should work, but it doesn’t. I go into the console, find the first  
GeneralMod, then do:

mod.GeneralModType = GeneralModType.new

mod.GeneralaModType.name = “blah”

mod.save

I get a saved mod type, but general\_mod\_type\_id is still nil. I’ve got  
another relationship in another two classes that’s working fine, so  
I’m guessing I’m just screwing up something basic here. This is a  
little higher level than I am used to, being a PHP schlub.

Any help?

---

<div class="post-metadata">

**Author:** ![Backov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Backov](https://www.ruby-forum.com/u/Backov)\
**Post date:** [February 18, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/problem-with-foreign-keys-not-working/129462/2 "2008-02-18T22:07:28Z")

</div>

On Feb 18, 2008, at 3:13 PM, Backov wrote:

> I’m a rails noob. 🙂
> 
> That said, I’ve been doing not too bad but am stumbling over a basic  
> problem, probably a naming issue. Here’s the steup:
> 
> class GeneralModType \< ActiveRecord::Base  
> has\_and\_belongs\_to\_many :GeneralMods  
> has\_many :general\_mods
> 
> end
> 
> class GeneralMod \< ActiveRecord::Base  
> belongs\_to :GeneralModType  
> belongs\_to :general\_mod\_type  
> .integer  
> end
> 
> Now, from all the research I am frantically doing, this seems like it  
> should work, but it doesn’t. I go into the console, find the first  
> GeneralMod,  
> meaning:  
> mod = GeneralMod.find(:first)  
> yes?

> then do:
> 
> mod.GeneralModType = GeneralModType.new

Huh? Your GeneralModType probably exists first in order for a  
GeneralMod to belong\_to it.

> Any help?

You also don’t show your migration to CreateGeneralModTypes, but I  
assume it is similar.

blah\_type = GeneralModType.new(:name =\> ‘blah’)  
blah\_type.save # or use .create rather than .new

mod = blah\_type.general\_mods.build(:name =\> “foo”, :rangebonus =\>  
2, :damagebonus =\> 1,  
:srdamagebonus =\> 0, :reloadbonus  
=\> 1, :accbonus =\> 2)  
mod.new\_record? # =\> true  
mod.general\_mod\_type\_id # == blah\_type.id

mod.save

Is that enough to help you up a rung?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Backov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c4cdca/32.png) [@Backov](https://www.ruby-forum.com/u/Backov)\
**Post date:** [February 18, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/problem-with-foreign-keys-not-working/129462/3 "2008-02-18T22:22:15Z")

</div>

Ya, after I posted I realized I screwed up with the HABTM thing, and  
changed it back to has\_many, but still using the camel case.

I tried it with the camel case first and got weird results and then  
changed it to the lower case variant you supplied and it’s all working  
now. What was happening there? Obviously it didn’t like camel case  
there, but it was sort of working… Not quite all the way, but sort  
of.

Thanks much anyway Rob. 🙂
