You don't have proper relationship. You should to take care of association and habtm migration, Have a look to below example, I have two model "User" and "Organization"
organization model
class Organization < ActiveRecord::Base
has_and_belongs_to_many :users,
:association_foreign_key => 'user_id',
:class_name => 'User',
:join_table => 'organizations_users'
attr_accessible :address, :name
end
user model
class User < ActiveRecord::Base
has_and_belongs_to_many :organizations,
:association_foreign_key => 'organization_id',
:class_name => 'Organization',
:join_table => 'organizations_users'
attr_accessible :name, :phone
end
Then need to create new OrganizationsUsers migration like below.
class CreateOrganizationsUsersTable < ActiveRecord::Migration
def self.up
create_table :organizations_users, :id => false do |t|
t.references :organization
t.references :user
end
add_index :organizations_users, [:organization_id, :user_id]
add_index :organizations_users, [:user_id, :organization_id]
end
def self.down
drop_table :organizations_users
end
end
You can get working example from here.