Skip to content

Instantly share code, notes, and snippets.

@hadrienblanc
Last active April 19, 2020 17:32
Show Gist options
  • Save hadrienblanc/48c762f7e8ced39a535731051e08a1c6 to your computer and use it in GitHub Desktop.
Save hadrienblanc/48c762f7e8ced39a535731051e08a1c6 to your computer and use it in GitHub Desktop.
active record
class Person < ActiveRecord::Base
belongs_to :location
belongs_to :role
end
class Role < ActiveRecord::Base
has_many :people
end
class Location < ActiveRecord::Base
has_many :people
end
Location.joins(:people)
Location.joins(people: :role)
Location.joins(people: :role).where(roles: { billable: true })
Location.joins(people: :role).where(roles: { billable: true }).distinct
Location.joins(:region).merge(Region.order(:name)).order(:name)
Location.from(Location.billable, :locations)
class Location < ActiveRecord::Base
def self.billable
joins(people: :role).where(roles: { billable: true }).distinct
end
def self.by_region_and_location_name
joins(:region).merge(Region.order(:name)).order(:name)
end
end
Location.from(Location.billable, :locations).by_region_and_location_name
class Person < ActiveRecord::Base
belongs_to :manager, class_name: "Person", foreign_key: :manager_id
has_many :employees, class_name: "Person", foreign_key: :manager_id
end
Person.
joins(:manager).
where.
not(managers_people: { id: Person.find_by!(name: "Eve") })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment