Skip to content

Instantly share code, notes, and snippets.

View cybersamx's full-sized avatar

Samuel Chow cybersamx

  • Los Angeles, Ca
  • 02:23 (UTC -07:00)
View GitHub Profile
@cybersamx
cybersamx / find_remove_old_activerecord.rb
Last active June 10, 2021 16:34
Find/Remove ActiveRecord older than n days
# Get objects older than 3 days.
@models = MyModel.where('created_at < :time', { time: 3.days.ago })
@models = MyModel.where(['created_at < ?', 3.days.ago])
# Get objects within the last 3 days.
@models = MyModel.where('created_at >= :time', { time: 3.days.ago })
@models = MyModel.where(['created_at >= ?', 3.days.ago])
# Multiple conditions.
@models = MyModel.where(['created_at >= ? and name like ?', 3.days.ago, 'Sam%'])
@cybersamx
cybersamx / update_without_touch.rb
Last active August 29, 2015 14:05
Update Rails ActiveRecord object without triggering validation and updating updated_at
object = MyModel.create(name: 'Sam')
# update_attribute bypass validation but will touch update_at
object.update_attribute(:name, 'Yo')
object.name == 'Yo' # true
object.name_changed? # false
# update_column bypass validation but will not touch update_at
object.update_column(:name, 'Yo')
object.name == 'Yo' # true
@cybersamx
cybersamx / curl_commands.sh
Last active August 29, 2015 14:05
Commonly used CURL commands for testing/debugging
# GET a JSON result.
curl -i -H "Accept: Application/json" -H "Content-Type: application/json" http://localhost/sessions.json
# POST a JSON result.
curl -X POST -H "Accept: Application/json" -H "Content-Type: application/json" -d '{"id":"IDVALUE","name":"Sam"}' http://localhost/sessions.json
# Use cookies (write/read).
curl -c cookies.txt -b cookies.txt http://localhost/users.json
# Follow redirection.
@cybersamx
cybersamx / activerecord_exotic_queries.rb
Created August 19, 2014 21:18
Rails ActiveRecord exotic data queries
# Unique values of a column
Address.uniq.pluck(:state)
@cybersamx
cybersamx / capybara_cheat_sheet.rb
Last active June 26, 2022 08:32
Capybara (visit/page) cheat sheet
# Navigation
visit('/resources')
visit(resource_path(resource))
# Clicking buttons and hyperlinks
click_button('Submit Button')
click_link('Link Text')
click_link('Link id')
click('Link or button')
@cybersamx
cybersamx / script_to_run_in_rails.rb
Last active July 26, 2023 18:40
Load and execute a Ruby script in Rails console
# Say you need to run this script which is not part of your Rails app...
# Do the following:
#
# 1. Assume we put this script file in <Rails app root>/tmp
# 2. $ cd <Rails app root>
# 3. $ rails console
# 4. ruby > load "#{Rails.root}/tmp/script_to_run_in_rails.rb"
# 5. ruby > MyClass.echo
# Hello
# => nil
@cybersamx
cybersamx / Install_SwaggerUI.md
Created September 29, 2014 22:56
Installing SwaggerUI

Steps for installing SwaggerUI on the Mac

  1. Clone SwaggerUI project
$ git clone git@github.com:wordnik/swagger-ui.git
  1. Install nginx (optioinal)
@cybersamx
cybersamx / edit.html.erb
Last active August 29, 2015 14:07
Rails select_tag and data validation
<%= form_for @my_model, html: { class: 'form-horizontal' } do |f| %>
<%= f.label :id, class: 'control-label %>
<div class="controls">
<%= f.select(:id, MyModel.all.pluck(:name, :id), include_blank: 'Select') %>
</div>
<% end %>
@cybersamx
cybersamx / rails_notice_vs_flash
Created October 22, 2014 01:25
Rails notice vs flash
# The mechanism for embedding a notification in a Rails controller is different for render and redirect.
def ping1
flash[:error] = 'Warning...'
render action: 'pong'
end
def ping2
redirect_to pong_path, notice: 'Warning...'
end
@cybersamx
cybersamx / logstash_forwarder_on_ubuntu.md
Last active December 27, 2018 02:18
Install Logstash Forwarder on Ubuntu

Installation

Add an apt repository and install the logstash-forwarder package.

$ sudo sh -c 'echo "deb http://packages.elasticsearch.org/logstashforwarder/debian stable main" > /etc/apt/sources.list.d/logstash.list'
$ wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install logstash-forwarder