不同命名空间中的关系

时间:2015-04-14 09:49:12

标签: ruby-on-rails ruby ruby-on-rails-3 models

我对如何在不同命名空间中的模型之间定义关系有点困惑。我创建了这个例子来说明我的问题,并根据我的要求添加了评论。

class User < ActiveRecord::Base
  has_one :account # meaning Account
end

class Account < ActiveRecord::Base
  belongs_to :user # meaning User
  has_many :campaigns # meaning campaigns
  has_one :account # meaning SomeCollection::Account
end

class Campaign < ActiveRecord::Base
  belongs_to :account # meaning Account
end

class SomeCollection::Account < ActiveRecord::Base
  belongs_to :account # meaning Account
  has_many :campaigns # meaning SomeCollection::Campaign
end

class SomeCollection::Campaign < ActiveRecord::Base
  belongs_to :account # meaning SomeCollection::Account
end

我最困惑的是AccountSomeCollection::Account之间的关系。我希望能够访问这样的实例 -

account.some_collection_account
# => <SomeCollection::Account ...>

1 个答案:

答案 0 :(得分:3)

您可以为每个关联指定一个类名,并选择关联名称,如下所示:

has_one :some_collection_account, class_name: 'SomeCollection::Account'