1
私は3つのモデル学校、(school.teachersまたはschool.studentsのように)正常に動作しますレール3にスコープ付きレコードを作成するにはどうすればよいですか?
しかし
-
の作り方レコードをフェッチ
人々と
役割
- school.teachers.build(または school.teachers.new)ロールを割り当てる 「先生」?
- school.students.build(または school.students.new)ロールを割り当てる 「学生」ですか?
class School < ActiveRecord::Base
has_many :teachers, :class_name => "Person", :include => :roles, :conditions => ["roles.name = ?",'Teacher']
has_many :students, :class_name => "Person", :include => :roles, :conditions => ["roles.name = ?",'Student']
end
class Person < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :school
scope :teachers, joins(:roles) & Role.teacher
scope :students, joins(:roles) & Role.student
end
class Role < ActiveRecord::Base
has_and_belongs_to_many :persons
validates_presence_of :name
def self.sanitize role
role.to_s.humanize.split(' ').each{ |word| word.capitalize! }.join(' ')
end
scope :teacher, where(:name => 'Teacher')
scope :student, where(:name => 'Student')
end
を持っています