2011-09-15 3 views
0

많은 이벤트가 있고 많은 그룹에 속한 User 클래스가 있습니다. 사용자가 이벤트를 만들고 그룹, 사용자 ID 또는 둘 모두에 의해 초대 할 사람을 제한 할 수있게하려고합니다.여러 모델에 대해 여러 개의 선택 상자가 레일에 표시됩니다.

그룹 및 개인을 모두 선택할 수있는 다중 선택 상자 태그는 어떻게 허용해야합니까? acts_as와 같은 것을 만들려고 생각하고 있지만 불필요하게 복잡해 보입니다.

답변

1

경고 !!! 첫 번째 반복 솔루션 - 더 간단해질 수 있습니다.

매핑을 수행하려면 다형성 관계가 필요할 수 있습니다.

Event 
    has_many :invitations 
    has_many :user_invitations, :class_name => "Invitation", :conditions => "inviteable_type = 'User'" 
    has_many :group_invitations, :class_name => "Invitation", :conditions => "inviteable_type = 'Group'" 


Invitation 
    belongs_to :inviteable, :polymorphic => true 
    #Invitations table would have inviteable_id, inviteable_type columns 
User 
    has_many :invitations, :as => :inviteable 
Group 
    has_many :invitations, :as => :inviteable 

지금 당신은 다르게 당신이 컬렉션에 대한 옵션을 이름을 는`Event.first.inviteables이보기에

(이 사용자 및 그룹의 컬렉션을 반환합니다) 예를 가질 수 있습니다

이벤트 모델에서
<select name='event[event_inviteables]' ...> 
<option value='group[][1]'>Group 1</option> 
.. 
<option value='user[][1]'>User 1</option> 

, 당신은 사용자가 어떤 것들을 결정하는 event_inviteables= 방법을해야하고, 어떤 사람은 그룹입니다 따라

을 inviteable 컬렉션을 업데이트3210
Event 
    def event_inviteables=(*args) 
    user_ids = #some magic to extract user_ids from args 
    group_ids = #some magic to extract group_ids from args 
    self.user_invitation_ids = user_ids 
    self.group_invitation_ids = group_ids 
    end 
+0

자세한 답변을 주셔서 감사합니다. 첫 번째 부분 (polymorphic association)이 있는데 Event.first.invitables를 호출 할 수 있습니다. 그러나 두 번째 부분, 즉 확인란 태그를 구현할 수 없습니다. (check_box와 혼란스러운 선택 상자에 대해 유감스럽게 생각합니다). check_box_tag에 대해 잘 알고 있듯이, ID 대신 배열 [id, class_name]을 전달해야합니다. invitable => {{id => "1", name => "User"}, {id => " 4 ", name =>"Group "}}. 확인란 태그에 대한 솔루션을 어떻게 숙달해야합니까? 다시 한번 감사드립니다. – AdamNYC

+0

체크 상자 태그는 이름 및 값만 사용합니다. <% = check_box_tag '이벤트 [event_inviteables] [user] []', user.id %><% = check_box_tag '이벤트 [event_inviteables] [group] []', group.id %>'(물론 이벤트 편집 페이지에서 사용자를 체크해야하는지 여부도 테스트해야합니다.) – tamersalama

관련 문제