Skip to content

Instantly share code, notes, and snippets.

View cisolarix's full-sized avatar
🌻
Cool

Yanming Deng cisolarix

🌻
Cool
  • NONE
  • Nanchang, Jiangxi, China
  • 09:08 (UTC +08:00)
View GitHub Profile

Transactions

As your business logic gets complex you may need to implement transactions. The classic example is a bank funds transfer from account A to account B. If the withdrawal from account A fails then the deposit to account B should either never take place or be rolled back.

Basics

All the complexity is handled by ActiveRecord::Transactions. Any model class or instance has a method named .transaction. When called and passed a block, that block will be executed inside a database transaction. If there's an exception raised, the transaction will automatically be rolled back.

Example

@cisolarix
cisolarix / 2015 年读的书.md
Last active August 29, 2015 14:14
2015 年读的书
@cisolarix
cisolarix / permission_actions.rb
Last active August 29, 2015 14:17
获取 permission actions 列表
controllers = Dir.new("#{Rails.root}/app/controllers/admin").entries
controllers.each do |controller|
if controller =~ /_controller/
cont = controller.camelize.gsub(".rb","")
cont1 = controller.gsub("_controller.rb", "")
(eval("Admin::#{cont}.action_methods")).sort.each {|met|
puts "admin/#{cont1}##{met}"
}
end
end
@cisolarix
cisolarix / seeds.rb
Last active August 29, 2015 14:17 — forked from seyhunak/seeds.rb
unless Rails.env.production?
connection = ActiveRecord::Base.connection
connection.tables.each do |table|
connection.execute("TRUNCATE #{table}") unless table == "schema_migrations"
end
sql = File.read('db/import.sql')
statements = sql.split(/;$/)
statements.pop
江西经济发展研究院
研究生学院
校办公室
国际学院
档案馆
信息管理学院
资产管理处
外国语学院
马克思主义学院
老干处
@cisolarix
cisolarix / xunlei_download_links_grabber.rb
Created May 10, 2015 02:38
xunlei_download_links_grabber
@cisolarix
cisolarix / live reload.md
Created June 18, 2015 05:45
live reload

bundle exec guard -P livereload

@cisolarix
cisolarix / Readme.md
Created July 28, 2017 02:40 — forked from mxstbr/Readme.md
Enable tab completion for JSX with Emmet in Atom

Enable tab completion for JSX with Emmet in Atom

This guide assumes you have the emmet and language-babel packages already installed in Atom

Gif of the tab completion working

  1. Open the keymap.cson file by clicking on Atom -> Keymap… in the menu bar
  2. Add these lines of code to your keymap:
'atom-text-editor[data-grammar~="jsx"]:not([mini])':
@cisolarix
cisolarix / config.rb
Created August 3, 2017 12:10 — forked from guiman/config.rb
Example mina deploy script using docker
require 'mina/git'
require 'mina/nginx'
set :application, 'your_app_name'
set :domain, 'your_server'
set :user, 'ubuntu'
set :deploy_to, '/location/to/deploy'
set :app_path, "#{deploy_to}/#{current_path}"
set :repository, 'your_repo'
set :branch, 'master'