1

我有一个连接两个模型的表有很多直通关系。有很多通过连接表 - 附加关联

用户必须通过当局的许多角色

我国政府表中有user_id的ROLE_ID firm_id

我希望每个机构(连接表),以与企业一对一的关系,三列。这可能吗?

---编辑或许多一点信息会使它更清晰。

我有很多“公司”

“用户”有很多企业通过“紧跟”

“用户”也有通过多'角色“当局”

我现在用的是cancan gem来限制用户的权利,以便他们可以关注任何公司,但他们只能编辑单个公司的详细信息。这意味着我不能在用户和公司之间建立直接的1对1关联,因为他们已经有很多关联 - 通过以下方式。 Current_user.firms将返回他们所关注的所有公司。

因此,我希望在Authority模型中加入他们拥有编辑权限的公司,并加入用户和角色。

https://docs.google.com/drawings/d/1CiKsUEdcS6hmKa23xsapWy33NS0Gp1f11a7TgaZbAy0/edit

这应该显示我的表格布局 - 虚线是联想我想作。

目前我的模特儿是这样的。

class Firm < ActiveRecord::Base 

has_one :authority 
has_many :follows, :dependent => :destroy 
has_many :users, :through => :follows 

class Follow < ActiveRecord::Base 
belongs_to :firm 
belongs_to :user 

class User < ActiveRecord::Base 
has_many :follows, :dependent => :destroy 
has_many :firms, :through => :follows 
has_many :roles, :through => :authorities 
has_many :authorities 

class Role < ActiveRecord::Base 
has_many :users, :through => :authorities 
has_many :authorities 

class Authority < ActiveRecord::Base 
belongs_to :users 
belongs_to :roles 
belongs_to :firm 

如果我能做到这一点如何将我去选择(在控制台我从工作的)特定的“权威”,并增加了“公司”。进一步更多我将如何阅读这个嵌套属性?

在此先感谢。

回答

1

我看不出为什么不。你的问题到底是什么?

class Authority < ActiveRecord::Base 
    belongs_to :firm 
end 

class Firm < ActiveRecord::Base 
    has_one :authority 
end 

你应该能够更新这样一个机构的firm_id:

authority = Authority.last 
authority.firm = Firm.last # or authority.update_attribute(:firm_id, Firm.last.id) 
authority.save 
+0

嗨罗宾,感谢您的答复,我加了一大堆的问题,以帮助精细。我试图按照建议设置数据库,并在上面列出,但我无法移植关联。一旦链接到用户和角色,我似乎无法将公司ID添加到特定的权威机构。 – RMcNairn

+0

我编辑过,如果它不起作用,告诉我错误是什么。 – Robin

+0

工作的一种享受,感谢罗宾非常赞赏 – RMcNairn