Skip to content

Instantly share code, notes, and snippets.

@fabiosussetto
Created July 23, 2011 11:57
Show Gist options
  • Save fabiosussetto/1101345 to your computer and use it in GitHub Desktop.
Save fabiosussetto/1101345 to your computer and use it in GitHub Desktop.
class Teacher < User
has_many :grades, :through => :teachings, :uniq => true
has_many :teachings
has_many :subjects, :through => :teachings, :uniq => true
def teaching_grades(organization)
grades.where('teachings.grade_id' => Grade.where(:organization_id => organization.id))
end
end
class Organization < ActiveRecord::Base
#attr_accessible :name, :slug
has_many :users, :through => :memberships
has_many :memberships
has_many :grades
has_many :subjects
has_many :events
has_many :attendances
has_many :resources
has_many :admins, :through => :memberships, :source => :user, :conditions => ["memberships.role = 'admin'"]
has_many :teachers, :through => :memberships, :source => :user, :conditions => ["memberships.role = 'teacher'"]
has_many :students, :through => :memberships, :source => :user, :conditions => ["memberships.role = 'student'"]
end
class Teaching < ActiveRecord::Base
#attr_accessible :teacher_id, :grade_id, :subject_id
belongs_to :subject
belongs_to :teacher
belongs_to :grade
validates :grade_id, :presence => true
validates :subject_id, :presence => true
scope :distinct_subjects, group(:subject_id).includes(:subject => :grades)
end
class Grade < ActiveRecord::Base
#attr_accessible :name, :institute_id
belongs_to :organization
has_many :teachers, :through => :teachings
has_many :subjects, :through => :teachings
has_many :teachings
has_many :students, :through => :attendances, :source => :user
has_many :attendances
has_many :resources, :through => :publications
has_many :publications
has_many :public_events, :through => :event_publications,
:class_name => 'Event',
:conditions => ['event_publications.public = ?', true]
has_many :event_publications
end
// generated queries:
Grade Load (1.4ms) SELECT DISTINCT `grades`.* FROM `grades` INNER JOIN `teachings` ON `grades`.id = `teachings`.grade_id WHERE `teachings`.`grade_id` IN (5, 6) AND ((`teachings`.teacher_id = 1)) LIMIT 1
Grade Load (0.5ms) SELECT DISTINCT `grades`.* FROM `grades` INNER JOIN `teachings` ON `grades`.id = `teachings`.grade_id WHERE `teachings`.`grade_id` IN (5, 6) AND ((`teachings`.teacher_id = 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment