Skip to content

Instantly share code, notes, and snippets.

@jgrannas
Created August 26, 2013 20:11
Show Gist options
  • Select an option

  • Save jgrannas/6346071 to your computer and use it in GitHub Desktop.

Select an option

Save jgrannas/6346071 to your computer and use it in GitHub Desktop.

Revisions

  1. jgrannas created this gist Aug 26, 2013.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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