简体   繁体   English

Rails中的Belongs_to和备份Emirates_to

[英]Belongs_to and backup belongs_to in Rails

This question comes from breaking up a question that was too big. 这个问题来自分解一个太大的问题。

Users create Events as a Role in a Network . UsersEvents创建为NetworkRole The Events are then connected to both the Role (different Users can occupy that Role when they are hired or fired) as well as the Event . 然后, Events既连接到Role (雇用或解雇不同Users也可以占据该Role ),也连接到Event Users that have administrative Roles can access all Events regardless of the Role that created the Event . 具有管理Roles Users可以访问所有Events而与创建EventRole无关。

It is possible that Role could be destroyed, but the Event will persist as long as the Network exists. Role可能会被破坏,但是只要Network存在, Event就将持续。 (Like if a position is deleted, but they still want the data for retired positions.) (就像职位被删除一样,但他们仍然希望获得退休职位的数据。)

So can the Event both belongs_to a Role and a Network separately? 那么, Event可以分别belongs_to一个Role和一个Network吗? Is that bad rails design? 那是不好的护栏设计吗? Is there another way to do this? 还有另一种方法吗? Would I have a special "Retired" role and keep events always tied to a Role and then use belongs_to :network, :through => :role ? 我是否将具有特殊的“退休”角色并将事件始终与Role绑定,然后使用belongs_to :network, :through => :role

Network.rb Network.rb

class Network < ActiveRecord::Base
  has_many :roles
  has_many :users, :through => :roles
  has_many :events
end

Role.rb Role.rb

class Role < ActiveRecord::Base
  belongs_to :user
  belongs_to :network
end

Event.rb - Is this right? Event.rb-是吗?

class Event < ActiveRecord::Base
  belongs_to :role  
  belongs to :network
end

In my opinion it's not bad design with your associations (Events belonging to both Role and Network). 在我看来,与您的关联(属于Role和Network的事件)的设计并不是一件坏事。 And I think it's more confusing to have a "retired Role" than have it NULL. 而且我认为拥有“退休角色”比拥有NULL更令人困惑。 But perhaps you should consider having a column status on the Role rather than deleting it. 但是也许您应该考虑在角色上具有列status ,而不是删除它。 If the status is not eg 1 (if 1 = ok and 2 = inactive as an example) it can't be used anymore, acting as it was deleted. 如果状态不是例如1 (例如1 =正常,2 =无效),则不能再使用,因为它已被删除。

It's difficult to give an advice when you don't have the whole picture, but, as I said, in my opinion it's no need for a "retired" Role just to be able to change the associations. 当您不了解全部情况时,很难给出建议,但是,正如我所说,我认为,不必为了改变关联而需要“退休”的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM