Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord 1个具有学生、导师角色的用户模型&x2B;学生、导师的不同情况?_Activerecord_Ruby On Rails 4_Polymorphic Associations - Fatal编程技术网

Activerecord 1个具有学生、导师角色的用户模型&x2B;学生、导师的不同情况?

Activerecord 1个具有学生、导师角色的用户模型&x2B;学生、导师的不同情况?,activerecord,ruby-on-rails-4,polymorphic-associations,Activerecord,Ruby On Rails 4,Polymorphic Associations,如何正确设置具有2个角色的用户模型,并为每个角色设置2个单独的概要文件模型?我对如何实施感到困惑。当前im正在使用此功能,但失败: 型号/用户.rb # id :integer ( only important columns noted to save space) # profile_id :integer # profile_type :string(255) belongs_to

如何正确设置具有2个角色的用户模型,并为每个角色设置2个单独的概要文件模型?我对如何实施感到困惑。当前im正在使用此功能,但失败:

型号/用户.rb

  #  id                     :integer ( only important columns noted to save space)
  #  profile_id             :integer
  #  profile_type           :string(255) 

  belongs_to :profile, :polymorphic => true
models/profile\u student.rb:

  #  user_id     :integer      
  has_one :user, as: :profile, dependent: :destroy
  #  user_id     :integer   
  has_one :user, as: :profile, dependent: :destroy
models/profile\u tutor.rb:

  #  user_id     :integer      
  has_one :user, as: :profile, dependent: :destroy
  #  user_id     :integer   
  has_one :user, as: :profile, dependent: :destroy
如何正确获取用户的配置文件?? 例如,使用design

@用户=当前用户配置文件


我会尝试使用两种类型的用户:学生和导师。为此,在用户表中,有一个名为type的列,并放入一个验证,以确保它是student或tutor:

validates :type, :inclusion => {:in => ['student', 'tutor']}
然后创建一个学生模型和一个导师模型。在rails中,“type”是一种特殊的属性,rails将知道它引用的是其他模型。然后,为了创建配置文件,您有两个选项。你可以说一个学生和一个导师都有一个档案,或者你可以把档案的类型分开

例如,您可以执行以下操作:

class Student < User
    has_one :profile
end

class Tutor < User
    has_one :profile
end
class学生
如果两个配置文件都有类似类型的信息,那么这可能适合您。但是,如果导师和学生的个人资料有很大不同,请尝试以下方法:

class Student < User
    has_one :student_profile
end

class Tutor < User
    has_one :tutor_profile
end
class学生
然后为每种类型的纵断面创建单独的模型

通过使用此“类型”列,可以使学生和导师继承用户的所有方法和属性,但也可以拥有自己独特的属性和方法