Created
December 14, 2016 17:17
-
-
Save stuartbates/61fda34f61b141028b76d3f5faaea5b7 to your computer and use it in GitHub Desktop.
A closer look at Ruby objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student < User | |
has_many :class_groups | |
def high_achiever? | |
average_score > 75 | |
end | |
end | |
class User < ActiveRecord::Base | |
include SchoolMember::InstanceMethods | |
extend SchoolMember::ClassMethods | |
attr_accessor :new_registration | |
def self.banned | |
where(banned: true) | |
end | |
def name | |
@name ||= "#{first_name} #{last_name}" | |
end | |
def based_in_uk? | |
country == 'UK' | |
end | |
end | |
module SchoolMember | |
module InstanceMethods | |
def has_parking_permit? | |
ParkingPermit.where(user_id: id).any? | |
end | |
end | |
end | |
module SchoolMember | |
module ClassMethods | |
def unassigned | |
where(class_count: 0) | |
end | |
end | |
end | |
student = Student.first | |
def student.debug | |
puts student.attributes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment