Skip to content

Instantly share code, notes, and snippets.

@jgrannas
jgrannas / MyModel.rb
Last active August 29, 2015 14:15
Connecting an external DB with Rails4 model using abstract class and establish connection
class MyModel < ActiveRecord::Base
# No corresponding table in the DB.
self.abstract_class = true
establish_connection(:ext_database)
def self.getCustomerId(first_name, last_name)
get = connection.select_one("SELECT * FROM customers WHERE First_Name=#{connection.quote(first_name)} AND Last_Name=#{connection.quote(last_name)}")
get.id
end
@jgrannas
jgrannas / gist:7236401
Last active December 27, 2015 00:18
Homes model, featured charts
class HomesController < ApplicationController
#load_and_authorize_resource
def index
@featured_charts = Chart.featured
#I want to have one object, and run through and run the below methods on each Chart.featured instead of seperate lines below
#If i try and run Chart.featured.items.each_car_fastest.ordered_by_et.topten i get error
@list1 = Chart.find(1).items.each_car_fastest.ordered_by_et.topten#dynamic_query("car_model","name","Supra (MKIV)").dynamic_query("transmission","name","Getrag 6-Speed (V160)").each_car_fastest.topten
@jgrannas
jgrannas / gist:6346071
Created August 26, 2013 20:11
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')
Started GET "/styles?utf8=%E2%9C%93&t%5B%5D=engagement" for 127.0.0.1 at 2013-08-09 13:38:51 -0400
Processing by StylesController#index as JSON
Parameters: {"utf8"=>"✓", "t"=>["engagement"]}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
 (0.4ms) SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = 1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))
Collection Load (0.3ms) SELECT "collections".* FROM "collections"
SQL (0.6ms) SELECT "styles"."id" AS t0_r0, "styles"."collection_id" AS t0_r1, "styles"."item_number" AS t0_r2, "styles"."name" AS t0_r3, "styles"."title" AS t0_r4, "styles"."description" AS t0_r5, "styles"."slug" AS t0_r6, "styles"."content" AS t0_r7, "styles"."created_at" AS t0_r8, "styles"."updated_at" AS t0_r9, "styles"."price" AS t0_r10, "styles"."data" AS t0_r11, "s
def self.filter_with_params(params)
scoped = self.where("styles.name != ''")
scoped = scoped.includes(:tags)#, :collection)
#params[:t].inject(scoped){|memo, val| memo.where(:tags => {:name => val})} if params[:t]
if params[:t]
params[:t].each do |t|
scoped = scoped.where(:tags => {:name => t})
end
end
scoped