Skip to content

Instantly share code, notes, and snippets.

@williamho
Created October 25, 2013 21:08
Show Gist options
  • Save williamho/7161864 to your computer and use it in GitHub Desktop.
Save williamho/7161864 to your computer and use it in GitHub Desktop.
class notes
course.third.io
guard:
foreman: start services e,g., elasticsearch, mysql
dotenv: load variables from .env file into rails environment variables
mailcatcher: useful for debugging mailers
rails admin
association scopes
presenters
commands (imperator gem) can be backgrounded
- Generally if speaking to third party service
-------
association proxies
bullet gem for eager loading debugging
- eager loading blocking?
- joins might be better if you need aggregation
- optimistic locking
after_commit (e.g., if doing background tasks, it may run before the data is actually in the db)
- resque, sidekiq
- stabby lambdas -> (one, two) { f(one, two) }
- use lambdas to avoid gotchas (if "30.minutes.ago" it should be run every time not cached with the class load, columns not existing)
scoping
- unscoped (also takes a block)
- reorder (removes all the order scopes), reverse order
- .limit(20) to get the actual videos: @videos = Video.popular.limit(20), later on page: "total videos: @videos.except(:limit).count"
- ambiguous column name, explicitly add table name
- scope attribute.to_s.pluralize, -> { where ("#{attribute_cents > 0") }
instead of pluck(:id) you can just do .ids, .video_ids, etc
postgres group issue
unloadable: refresh whenever this file changes without having to reload the server
increment_counter :column, ids
association extensions
- has_many :blah, extend: NameOfModule
with_options
sti
polymorphism -> purchases { item_id: 123, item_type: [product, user, ..]
- belongs_to: :item
- has_many :purchases, source: :item //, foreign_key: :item_id
- product has… foreign_key: :item_id
Caching
1. Model caching (all users)
2. HTTP caching (1 user)
caching collections: include count in cache key to deal with deleting objects in collection
- cache the cache key and flush on save/destroy/touch (Good for collections that don't change often)
- cache parts of views for faster page generation
- user-specific features put in javascript, cache everything else
- *** multifetch fragments ***
Project.benchmark("blah") do… // activerecord
self.class.benchmark("blah") // controller
benchmark("blah") // view
rails-footnotes
--------------------
# attr_accessible only affects things passed in via hash.
attr_accessible :att, as: :super_admin
@user.assign_attributes(params[:user], as: :super_admin)
strong parameters in rails 4
**** force_ssl ****
if cookie served over regular http, consider it compromised
cookies have size limit
encrypted cookie_store
csrf: submit form on other site, posts form to your own site
form_for addresses csrf with a hidden field while form_tag does not (unless :authenticity_token)
protect_from_forgery in applicationcontroller
for ajax_requests, find fields with token in it, send it with post request
google prefetching for GET requests -- people who use get to do things like deleting
before_filter match ip range
config.filter_parameters += [:password] in application.rb // don't log passwords
ruby regex matches single lines only with /^blah$/ otherwise use /\Ablah\z/
if just Blah.find(:id).destroy you can delete things from other users. @current_user.projects.find(params[:id])
jenkins vs circle
nightly db backups?
- notifications send to some stream (e.g., twitter)
geckoboard
airbrake (track app exceptions) -> begin … rescue Airbrake.notify $! end
bundle --without development,test
pow.cx
boxen
routing namespaces (can also set subdomain constraints)
inflect.acronym 'API'
engines -- apps mounted inside other apps (e.g., assets, railsadmin)
bootsy, forem, etc
create a plugin with --mountable, generates a dummy application that uses your engine
helpers are namespaced by being included in actionview instance, not a global namespace
revert migrations for scope with rake db:migrate scope=bootsy VERSION=0
if you need assets for an engine, tell rails to precompile
user/time specific stuff don't cache but use javascript to display
debugger
rails generators: lib/generators
heredocs in ruby: <<-NAME text goes here NAME
ActiveSupport:
.presence, .blank?, .present?, .duplicable?, config[:host].try(:upcase), {a:123,c:234}.to_query
def process *args
options = args.extract_options! # If last arg is a hash, shift into options
options.merge(blah: true)
do_stuff_with_args(*args)
end
ZEUS!
http://www.codebeerstartups.com/2013/04/must-have-gems-for-development-machine-in-ruby-on-rails/
find_by_slug -> if nil, get result with shortest edit distance and return that one
d(slug, o) < (1.25 * slug.length).ceil # If a few characters off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment