Skip to content

Instantly share code, notes, and snippets.

@jgrannas
Created August 26, 2013 20:11
Show Gist options
  • Save jgrannas/6346071 to your computer and use it in GitHub Desktop.
Save jgrannas/6346071 to your computer and use it in GitHub Desktop.
Timeslip - trying to limit method to one timeslip per car
class Timeslip < ActiveRecord::Base
belongs_to :car
belongs_to :user
has_one :car_model, through: :car
has_one :transmission, through: :car
has_one :engine, through: :car
default_scope order('et1320 ASC')
def youtube_id
regex = /^(?:http:\/\/)?(?:www\.)?\w*\.\w*\/(?:watch\?v=)?((?:p\/)?[\w\-]+)/
video_url.match(regex)[1]
end
def self.by_carmodel(carmodel)
scoped = joins(:car_model).where(:car_models => {:name => carmodel})
scoped
end
def self.by_trans(trans)
scoped = joins(:transmission).where(:transmissions => {:name => trans})
scoped
end
def self.one_per_car
scoped = unscoped.select("car_id").group("car_id")
scoped
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment