【问题标题】:Rspec does not load the objects through has_and_belongs_to_many associationRspec 不通过​​ h​​as_and_belongs_to_many 关联加载对象
【发布时间】:2012-08-29 15:00:03
【问题描述】:

我的模型中有这个:

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

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

【问题讨论】:

    标签: ruby-on-rails-3 activerecord rspec


    【解决方案1】:

    在用户模型中你应该写

    has_and_belongs_to_many  :instances
    

    而不是

      has_many :instances
    

    【讨论】:

    • 感谢您的回答,但这并不能解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多