나는 올바른 길을 가고 있는지 그리고 레일이 이런 종류의 것을 허용하는지 알고 싶습니다.레일 has_many : through, null belongs_to, 다수 belongs_to 및 belongs_to 삭제?
User
은 Network
에서 하나의 Role
을 갖는다. 즉 "짐"은 "학교"에서 "역사 교사"입니다.
Role
에는 위치 (전원)와 이름 (레이블)이 모두 있습니다. "짐"은 "역사 교사"(레이블)이지만 회원, 관리자 또는 감독자 또는 기타 (권력)의 권한을가집니다.
는 Role
User
에 따라 그/그녀가 그들을 생성 여부 Network
의 모든 Events
을 볼 수 있습니다. Jim이 "Principal"(관리자)이지만 "Jim"이 "History Teacher"(회원) 인 경우 "Jim"은 "Nancy 's" "Recess Plan"을 볼 수 있습니다.
User
은 을 내에 Role
으로 생성합니다. 즉 "Jim"은 "School"에서 "History Teacher"로 "Lesson Plan"을 만듭니다.
Event
영원히 특정 Network
와 현재 해당 Role
에 연결되어 있습니다.
은 내가 User
가 Role
다른 User
을 대체하는 경우 Event
가 지속하고, 새로운 User
가 Event
에 액세스 할 수 있도록합니다. 즉 "Tom"은 "Jim"을 "History Teacher"로, "History Teacher"이기 때문에 "Lesson Plan"을 수정할 수 있습니다. "Jim"은 더 이상 "수업 계획"에 액세스 할 수 없습니다.
은 그러나 나는 또한 Role
에 첨부 된 User
가없는 경우 Event
가 계속하고자합니다. 즉 "Tom"이 (가) 해고되고 현재 교체가 없으면 관리자는 여전히 "수업 계획"을 볼 수 있습니다. 그 Role
이 삭제 된 경우 Role
없이 그 Network
에 연결로
마지막의 Event
는 여전히 존재합니다.
모델은 내가 허가 캉캉를 사용하고, 다음과 같습니다, 이들은 나의 질문은 다음과 같습니다
- 것은 역할은
User
누락 수 있습니다, 또는 좀 일반적인 "없음"User
또는 "을 만들 수 없습니다해야 모두 "User
? 그리고Event
은Role
가 누락 될 수 있습니까? (속할 수 있을까?) Event
을Role
과Network
에 모두 연결하는 것이 좋은가요? 이 작업을 수행하는 더 좋은 방법이 있습니까?User
하면- 가에 따라 더 많은 이벤트를 볼 수있는 그/그녀의
Role
그들이 가지고있는 않는Network
또는Role
을 통해 많은Events
? 나는Network
을 통해 생각하고 있으며 Ability.rb는 그것을 제한 할 것입니다.
User.rb
class User < ActiveRecord::Base
has_many :roles
has_many :networks, :through => :roles
has_many :events, :through => :network
# I would use CanCan to determine the authorization of
# what network events they can see based on their role?
end
네트워크.기술적으로 nil
될 수 RB
class Network < ActiveRecord::Base
has_many :roles
has_many :users, :through => :roles
has_many :events
# it shouldn't have this through roles right?
# because a role can be deleted
end
Role.rb
class Role < ActiveRecord::Base
belongs_to :user #CAN THIS BE NULL?
belongs_to :network
end
Event.rb
class Event < ActiveRecord::Base
belongs_to :role #Can this be null?
belongs to :network
# Does it belong to the network through the role,
# or can it belong on its own, in case the role is deleted?
# belongs_to :user, :through => :roles
# Is this necessary if I am using CanCan
# to determine if a User can reach the event?
end
Ability.rb는
if user
user.roles.each do |role|
can :manage, Event, :role_id => role.id
if role.position == "admin" || role.position == "manager"
can :manage, Event, :network_id => role.network_id
elseif role.position == "supervisor"
can :read, Event, :network_id => role.network_id
end
end
end
귀하의 질문은 정말로 무겁습니다. 더 쉽게 설명 할 수있는 작은 문제로 나누어 더 나은 응답을 얻을 수 있습니다. – aguynamedloren
감사합니다. 나는 그 일을하는 법을 계속 지키고 있었지만 확실하지는 않았다. – dewyze