Last active
April 19, 2020 17:32
-
-
Save hadrienblanc/48c762f7e8ced39a535731051e08a1c6 to your computer and use it in GitHub Desktop.
active record
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 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 |
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
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 |
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 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