Problem with foreign keys not working..

I'm a rails noob. :slight_smile:

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?

I'm a rails noob. :slight_smile:

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

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,

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.

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?

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 Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com

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. :slight_smile: