Created
September 12, 2018 15:59
-
-
Save bthooper/82d089fd751f86da2a136591f3afcdac to your computer and use it in GitHub Desktop.
Trying to understand arel better.....
This file contains 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 Boat < ActiveRecord::Base | |
belongs_to :captain | |
has_many :boat_classifications | |
has_many :classifications, through: :boat_classifications | |
def self.first_five | |
self.limit(5) | |
end | |
def self.dinghy | |
self.where("length <= ?", 20) | |
end | |
def self.ship | |
self.where("length >?", 20) | |
end | |
def self.last_three_alphabetically | |
order(name: :desc).limit(3) | |
end | |
def self.without_a_captain | |
where(captain: nil) | |
end | |
def self.sailboats | |
joins(:classifications).where(classifications: {name: "Sailboat"}) | |
end | |
def self.with_three_classifications | |
where(joins(:classifications).group(:id).having("count(*) = 3")) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment