views:

645

answers:

1

So I just started my first rails project yesterday. I had two many-to-many (has_and_belongs_to_many) relationships in my application. I had one between models games and teams and another between models stats and results. This was all working just fine by creating the join table myself with a migration.

I then decided that I did not want to have the stats/results relationship be many-to-many but what to many instead so I ran the following migration and switched the relationships to has_many and belongs_to:

class FixingResultStatRelationship < ActiveRecord::Migration
  def self.up
    add_column :results, :stat_id, :integer
    drop_table "results_stats"
  end

  def self.down
   remove_column :results, :stat_id
   create_table "results_stats", :id => false do |t|
      t.column "result_id", :integer
      t.column "stat_id", :integer
    end
    add_index "results_stats", "result_id"
    add_index "results_stats", "stat_id"
  end
end

Then when I do this it not only does the new one-to-many relationship not work as I get a NoMethodError when calling say Result.find(0).stat but now my previous working many-to-many relationship with games and teams is now broken as well. I used to be able to call Game.fine(0).teams just find and see the results and now I get a NoMethodError as well. Any thoughts? I am quite lost and any help would be great.

A: 

I just realized that I had belongs_to :stats rather than the singular stat. I fixed this and reloaded the server and everything works fine now. Thanks anyways though Abie.