Ruby on rails 如何通过rails关联更改has_的方法名称?
我试图创建两个与同一个目标表有许多关系的表,但在定义关联时无法覆盖rails创建的默认方法名 情况如下:Ruby on rails 如何通过rails关联更改has_的方法名称?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,我试图创建两个与同一个目标表有许多关系的表,但在定义关联时无法覆盖rails创建的默认方法名 情况如下: class User < ApplicationRecord has_many :conference_attendees, dependent: :destroy has_many :conference_organizers, dependent: :destroy has_many :conferences, through: :conference_attendee
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
class用户
我尝试访问用户参加的所有会议,然后访问用户使用以下方式组织的所有会议:
User.find(id: <id>).organizing
User.find(id: <id>).attending
User.find(id:).org
User.find(id:).atting
但我无法做到
User.find(id: <id>).conferences
User.find(id:).conferences
默认为组织会议。如何访问参加会议?如果需要两个关于用户和会议的联接表,可以这样设置
# 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 %>
# 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 %>
#app/controllers/conferences_controller.rb
def秀
@conference=conference.find(参数[:id])
@与会者=@conference.attendes
@组织者=@conference.organizers
结束
#app/views/conferences/show.html.erb
组织者
与会者
然后在用户的显示页面上显示会议:
# 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 %>
# 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 %>
#app/controllers/users_controller.rb
def秀
@user=user.find(参数[:id])
@出席=@user.atting
@Organization=@user.organization
结束
#app/views/users/show.html.erb
会议组织
日期:
出席会议
日期:
也就是说,使用一个联接表可能比使用两个联接表更干净。我将把它放在一个单独的答案中。因为您在相同的两个表上进行联接,所以可能更干净的做法是只进行一个联接表,然后添加一个名为status的字段来区分组织者和与会者。这样,您可以添加其他状态值,例如“presenter” 设置表格
railsg型号注册用户:参考会议:参考状态:string
<代码>rake db:迁移
设置模型关联:
# 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])
@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 %>
# 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 %>
#app/controllers/conferences_controller.rb
def秀
@conference=conference.find(参数[:id])
@与会者注册=@conference.registrations.attendes
@组织者注册=@conference.registrations.organizers
结束
#app/views/conferences/show.html.erb
组织者
与会者
然后在用户的显示页面上显示会议:
# 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 %>
# 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 %>
#app/controllers/users_controller.rb
def秀
@user=user.find(参数[:id])
@出席者注册=@user.registrations.attenders
@组织注册=@user.registrations.organizers
结束
#app/views/users/show.html.erb
会议组织
日期:
出席会议
日期: