多对多的联想

时间:2012-04-29 00:54:50

标签: ruby-on-rails

所以我有一个代表情况的模型:

  • 学生参加比赛。
  • 比赛测量某些技能。
  • 每次比赛,每位学生都会获得每项技能的分数。

以下是我的模特:

class Student < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  attr_accessible :name
  has_and_belongs_to_many :skills
  has_many :contest_participations
  has_many :contests, :through => :contest_participations
  has_many :contest_scores, :through => :contests

end

class Skill < ActiveRecord::Base
  attr_accessible :name
  has_and_belongs_to_many :students
end

class Contest < ActiveRecord::Base
  attr_accessible :name, :contest_participations_attributes, :contest_scores_attributes
  has_many :contest_participations
  has_many :students, :through => :contest_participations
  has_many :contest_skills
  has_many :skills, :through => :contest_skills
  has_many :contest_scores
  accepts_nested_attributes_for :contest_participations, :contest_scores


end

class ContestParticipation < ActiveRecord::Base
  attr_accessible :contest_id, :student_id
  belongs_to :student
  belongs_to :contest
end

class ContestScore < ActiveRecord::Base
  attr_accessible :contest_id, :score, :skill_id, :student_id
  has_and_belongs_to_many :contests
  has_and_belongs_to_many :skills
  has_and_belongs_to_many :student
end

在我的竞赛编辑视图中,我正在尝试创建一个带有formtastic的表单,该表单将显示所有参赛者,并允许用户为比赛中的每个技能添加分数,如下所示。

但我收到错误(student_id无效符号)。如何更新学生成绩?

<%= semantic_form_for @contest do |f| %>

    <%= f.input :name %>

    <%= @contest.students.each do |student| %>
         <%= @contest.skills.each do |skill| %>
               <%= f.inputs :name => "Scores", :for => :contest_scores do |scores_form| %>
                     <%= scores_form.input :student_id => "student.id" %>
                     <%= scores_form.input :skill_id => "skill.id" %>
                     <%= scores_form.input :score, :label => "Score" %>
               <% end %>
               <br />                 
         <% end %>
    <% end %>

    <%= f.actions do %>
         <%= f.action :submit, :as => :button %>
    <% end %>

<% end %>

1 个答案:

答案 0 :(得分:0)

试试这个。添加隐藏因为e认为你不想让id出现

<%= scores_form.input :student_id, :as => :hidden, :value => student.id %>
<%= scores_form.input :skill_id, :as => :hidden, :value => skill.id %>