2017-04-05 42 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/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/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/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/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 

然後顯示在會議展示頁面上的參加者和組織者:

# 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/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 %> 

這是說,它可能使用一個連接表而不是兩個更清潔。我會把它放在一個單獨的答案中。