0

我的模型中有这个:

class Instance < ActiveRecord::Base
   has_and_belongs_to_many :owners, :class_name => 'User'

和这个:

 class User < ActiveRecord::Base
   has_many :instances

我有这个迁移:

 class CreateInstancesUsersJoinTable < ActiveRecord::Migration
     def up
       create_table :instances_users, :id=>false do |t|
         t.string :instance_id
         t.string :user_id
       end
     end

 def down
   drop_table :instances_users
  end
end

在实例控制器中,我有:

   @instance.owners << owner

但是测试表明所有者不在所有者数组中。但是当我说:

  p @instace.owners - before or after @instance.owners << owner

测试通过。有谁知道为什么会这样?

4

1 回答 1

0

在用户模型中,您应该编写

has_and_belongs_to_many  :instances

代替

  has_many :instances
于 2012-08-29T15:08:01.687 回答