2017-04-05 3 views
0

두 대상을 만들 때 동일한 대상 테이블에 많은 관계가 있지만 연결 정의시 레일스가 만드는 기본 메서드 이름을 재정의 할 수 없습니다.has_through 레일 연관의 메소드 이름을 변경하는 방법은 무엇입니까?

class User < ApplicationRecord 
    has_many :conference_attendees, dependent: :destroy 
    has_many :conference_organizers, dependent: :destroy 
    has_many :conferences, through: :conference_attendees, class_name: 'attending', dependent: :destroy 
    has_many :conferences, through: :conference_organizers, source: :conference, dependent: :destroy 

class ConferenceOrganizer < ApplicationRecord 
    alias_attribute :organizers, :users 
    belongs_to :conference 
    belongs_to :user 
end 

class ConferenceAttendee < ApplicationRecord 
    belongs_to :conference 
    belongs_to :user 
end 

class Conference < ApplicationRecord 
    has_many :conference_attendees, dependent: :destroy 
    has_many :conference_organizers, dependent: :destroy 
    has_many :users, through: :conference_attendees, dependent: :destroy 
    has_many :organizers, through: :conference_organizers, source: :user, dependent: :destroy 

나는 모든 사용자가 참석하는 회의와 사용자가 다음과 같은 것을 사용하여 구성하는 모든 회의에 액세스하려고 :

다음

는 경우가

User.find(id: <id>).organizing 
User.find(id: <id>).attending 

을하지만, 나는 할 수 없습니다.

User.find(id: <id>).conferences 

기본적으로 조직 회의입니다. 참석 컨퍼런스에 어떻게 액세스합니까?

답변

0

동일한 두 테이블에 대한 조인을 수행 중이므로 하나의 조인 테이블을 수행하고 주최자를 참석자와 구분하기 위해 status라는 필드를 추가하는 것이 더 깔끔할 수 있습니다. 그렇게하면 "발표자"와 같은 다른 상태 값을 추가 할 수 있습니다.

테이블 설정 rails g model Registration user:references conference:references status:string; rake db:migrate

이 설정

모델 협회 : 사용자의 쇼 페이지에서 회의를 보여주기 위해 다음

# app/controllers/conferences_controller.rb 
def show 
    @conference = Conference.find(params[:id]) 
    @attendee_registrations = @conference.registrations.attendees 
    @organizer_registrations = @conference.registrations.organizers 
end 

# app/views/conferences/show.html.erb 
<h3>Organizers</h3> 
<% @organizer_registrations.each do |registration| %> 
    <li><%= link_to(registration.user.name, registration.user) %></li> 
<% end %> 
<h3>Attendees</h3> 
<% @attendee_registrations.each do |registration| %> 
    <li><%= link_to(registration.user.name, registration.user) %></li> 
<% end %> 

:

# app/models/user.rb 
has_many :registrations, dependent: :destroy 

# app/models/conference.rb 
has_many :registrations, dependent: :destroy 

# app/models/registration.rb 
belongs_to :user 
belongs_to :conference 
scope :attendees, -> { where(status: "Attendee") } 
scope :organizers, -> { where(status: "Organizer") } 

다음 회의 쇼 페이지에서 참석자와 주최자를 보여

# app/controllers/users_controller.rb 
def show 
    @user = User.find(params[:id]) 
    @attending_registrations = @user.registrations.attendees 
    @organizing_registrations = @user.registrations.organizers 
end 

# app/views/users/show.html.erb 
<h3>Conferences Organizing</h3> 
<% @organizing_registrations.each do |registration| %> 
    <li><%= link_to(registration.conference.name, registration.conference) %><br> 
    Date: <%= registration.conference.date %></li> 
<% end %> 
<h3>Conferences Attending</h3> 
<% @attending_registrations.each do |registration| %> 
    <li><%= link_to(registration.conference.name, registration.conference) %><br> 
    Date: <%= registration.conference.date %></li> 
<% end %> 
0

사용자 및 회의에 두 개의 조인 테이블이 필요하면이 방법으로 설정할 수 있습니다. 되고 그것이 수도 말했다

# app/controllers/users_controller.rb 
def show 
    @user = User.find(params[:id]) 
    @attending = @user.attending 
    @organizing = @user.organizing  
end 

# app/views/users/show.html.erb 
<h3>Conferences Organizing</h3> 
<% @organizing.each do |conference| %> 
    <li><%= link_to(conference.name, conference) %><br> Date: <%= conference.date %></li> 
<% end %> 
<h3>Conferences Attending</h3> 
<% @attending.each do |conference| %> 
    <li><%= link_to(conference.name, conference) %><br> Date: <%= conference.date %></li> 
<% end %> 

:

다음
# app/controllers/conferences_controller.rb 
def show 
    @conference = Conference.find(params[:id]) 
    @attendees = @conference.attendees 
    @organizers = @conference.organizers 
end 

# app/views/conferences/show.html.erb 
<h3>Organizers</h3> 
<% @organizers.each do |user| %> 
    <li><%= user.name %></li> 
<% end %> 
<h3>Attendees</h3> 
<% @attendees.each do |user| %> 
    <li><%= user.name %></li> 
<% end %> 

는 사용자의 쇼 페이지에 회의를 표시하려면

# app/models/user.rb 
has_many :conference_attendees, dependent: :destroy 
has_many :conference_organizers, dependent: :destroy 
has_many :attending, through: :conference_attendees, source: :conference 
has_many :organizing, through: :conference_organizers, source: :conference 

# app/models/conference.rb 
has_many :conference_attendees, dependent: :destroy 
has_many :conference_organizers, dependent: :destroy 
has_many :attendees, through: :conference_attendees, source: :user 
has_many :organizers, through: :conference_organizers, source: :user 

# app/models/conference_attendee.rb 
belongs_to :user 
belongs_to :conference 

# app/models/conference_organizer.rb 
belongs_to :user 
belongs_to :conference 

그런 다음 회의 쇼 페이지에서 참석자와 주최자를 표시합니다 두 개가 아닌 하나의 조인 테이블을 사용하려면 클리너를 사용하십시오. 나는 그것을 별도의 대답으로 넣을 것이다.