简体   繁体   English

如何有效地管理与关联模型的多态关联?

[英]How do I Resourcefully manage a polymorphic association with an associated model?

I am trying to do the following: events can either have individual students compete, or teams of students. 我正在尝试执行以下操作:事件可以使单个学生竞争,也可以使一组学生竞争。 So I set it up polymorphically, as follows: 因此,我对它进行了多态设置,如下所示:

class Event < ActiveRecord::Base
  belongs_to :event_type
  has_many :competitors
  has_many :students, through: :competitors, :source => :participant, :source_type => 'Student'
end

class Competitor < ActiveRecord::Base
  belongs_to :event
  belongs_to :participant, polymorphic: true

  # also has other attributes, like points, and grade
end

class Student < ActiveRecord::Base
  has_many :competitors, :as => :participant
  has_many :events, :through => :competitors
end

class Team < ActiveRecord::Base
  has_many :competitors, :as => :participant
  has_many :events, :through => :competitors
  has_many :team_members
  has_many :students, :through => :team_members
end

tl;dr: How do I work with these using Resourceful controllers? tl; dr:如何使用足智多谋的控制器处理这些?

I already have an events controller which allows me to CRUD it's properties. 我已经有一个事件控制器,它使我可以CRUD它的属性。 I have a Students controller that does the same. 我有一个学生控制器,执行相同的操作。 So now I need the Competitors controller to combine them. 因此,现在我需要“竞争者”控制器来组合它们。 And it's there that I draw a blank. 我在这里画一个空白。

What I'd like is to be able to choose an events competitors on one screen - from a list of student checkboxes. 我想要的是能够从一个学生复选框的列表中选择一个屏幕上的比赛项目。 If I create a nested route like 如果我创建一个嵌套路由,例如

resources :events, shallow: true do
  resources :competitors, shallow: true
end

I can then go to events/1/competitors to show the list of competitors. 然后,我可以转到events/1/competitors以显示竞争对手列表。 But then should it show Teams, or Students? 但是,然后它应该显示团队,还是学生? I don't want it to become a mess of if/elses. 我不想让它变成一堆if / elses。 So should I have separate controllers for those two? 那么我应该为这两个单独设置控制器吗? How would they be named? 他们将如何命名? And how would I interact with them? 我将如何与他们互动?

Let's say I do make a controller specifically for student competitors: 假设我确实为学生竞争对手制作了一个控制器:

resources :events, shallow: true do
  resources :student_competitors, shallow: true
end

I'd get events/1/student_competitors . 我会得到events/1/student_competitors This would show a list of all students competing in that event. 这将显示所有参加该事件的学生的列表。 Let's say I wanted to make that list a set of checkboxes with an Update button. 假设我想通过“更新”按钮使该列表成为一组复选框。 How would I construct that form? 我将如何构造该表格?

<%= simple_form_for(@event, :html => { :class => 'form-vertical' }) do |f| %>
  <% @students.each do |student| %>
    <%= check_box_tag "event[student_ids][]", student.id, student.events.include?(@event) %>
    <span><%= student.name %></span>
  <% end %>

  <%= f.button :submit, 'Update Competitors' %>
<% end %>

This would submit the form back to events/1 , whereas I'd like it to be submitted to the StudentCompetitors controller, but I can't call simple_form_for with a collection like simple_form_for(@event, @competitors) and have it construct a path to the update action. 这会将表单提交回events/1 ,而我希望将其提交给StudentCompetitors控制器,但是我无法使用诸如simple_form_for(@event, @competitors)类的集合调用simple_form_for并使其构造路径进行更新操作。

Essentially, I'm not sure how best to go about all this. 本质上,我不确定如何最好地完成所有这些工作。 Am I trying to be too strict, sticking to a resources? 我是否要过于严格,坚持使用资源? Your help is appreciated. 感谢您的帮助。

I used a singular resource: 我使用了单个资源:

resources :events, shallow: true do
  resource :student_competitors, shallow: true
end

Thus I can edit student competitors at events/1/student_competitors , and I use simple_form_for as: 因此,我可以在events/1/student_competitors编辑学生竞争者,并且将simple_form_for用作:

<%= simple_form_for(:student_competitors, url: event_student_competitors_path, :method => :put, :html => { :class => 'form-vertical' }) do |f| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM