0

使用匹配创建学生都是模型,我想将学生的值存储在匹配中。应该如何解决这个问题?

我尝试了委托,但抛出了匹配为空的错误,有什么想法吗?谢谢!

用户模型:

class Student < ActiveRecord::Base

  attr_accessible :name, :level

  has_one :match
  before_create :setup_match

  def setup_match
    self.create_match # create the match that belongs to this student
  end


end

匹配型号:

class Match < ActiveRecord::Base

  belongs_to :student
  attr_accessible :initiated, :level

  before_save :default_values

  def default_values
    # HERE is the problem
    # Need to store student.name and student.level here, how?
    self.initiated = student.name
    self.level = student.level
  end


end
4

1 回答 1

1

您应该在 Student 中移动 Match 的设置值.. 像这样(未测试):

class Student < AR::Base
  has_one :match
  accepts_nested_attributes_for :match

  before_create :setup_match

  def setup_match
    build_match(:initiated => name, :level => level)
  end
end
于 2013-05-31T23:53:14.600 回答