class User < ActiveRecord::Base
belongs_to :school
validates :email, :email => { :message => "Must be a valid email." }, :format => { :with => /\A[\w+\-.]+@#{Regexp.quote(school.email_domain)}\z/i }
end
我希望能够验证创建用户的电子邮件是否与他们学校的电子邮件域匹配。我通过以下方式创建用户:
@school.users.create(params[:user])
抛出错误:
undefined local variable or method `school' for #<Class:0x007f8aaabb0df0>
谢谢您的帮助!