Skip to content

Instantly share code, notes, and snippets.

@bthooper
Created September 12, 2018 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bthooper/82d089fd751f86da2a136591f3afcdac to your computer and use it in GitHub Desktop.
Save bthooper/82d089fd751f86da2a136591f3afcdac to your computer and use it in GitHub Desktop.
Trying to understand arel better.....
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