Skip to content

Instantly share code, notes, and snippets.

module MyModel
extend ActiveSupport::Concern
included do
__send__(:include, ActiveModel::Conversion)
__send__(:include, ActiveModel::Validations)
extend ActiveModel::Naming
extend ActiveModel::Translation
class << self
alias_method_chain :i18n_scope, :my_model
module MyModel
extend ActiveSupport::Concern
included do
__send__(:include, ActiveModel::Conversion)
__send__(:include, ActiveModel::Validations)
extend ActiveModel::Naming
extend ActiveModel::Translation
end
@pinzolo
pinzolo / taggable.rb
Last active August 29, 2015 14:04
polymorphic を利用した has_many な関連を付与する
# xxx_create_tags.rb
class CreateTags < ActiveRecord::Migration
def change
create_table :tags do |t|
t.string :name, null: false
end
add_index :tags, :name, unique: true
end
end
@pinzolo
pinzolo / notable.rb
Last active August 29, 2015 14:04
polymorphic を利用した has_one な関連を付与する
# xxx_create_notes.rb
class CreateNotes< ActiveRecord::Migration
def change
create_table :notes do |t|
t.references :notable, polymorphic: true, null: false
t.text :note
end
add_index :notes, [:notable_type, :notable_id], unique: true
end
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "centos65"
# HOSTから接続するため有効化
#!/bin/sh
# This script is deployed as /var/lib/rails/update_redmine.sh
if [ $# -eq 0 ]; then
echo Enter Redmine version.
exit 1
fi
REDMINE_VERSION=$1
@pinzolo
pinzolo / active_record_methods_with_exclamtion.rb
Last active August 29, 2015 14:03
ActiveRecord における ! つきメソッドの挙動まとめ
@user = User.new
# ========== 例外発生系 ==========
# 保存する
@user.save
# 保存に失敗したら例外を発生させる
@user.save!
# 更新する
Pry::DEFAULT_HOOKS.add_hook(:before_session, :gem_auto_require) do |out, target, _pry_|
dir = `pwd`.chomp
gem_name = File.basename(dir)
if File.exist?(File.join(dir, "#{gem_name}.gemspec"))
lib = File.join(dir, 'lib')
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
if File.exist?(File.join(lib, "#{gem_name}.rb"))
begin
require gem_name
rescue LoadError => e
@pinzolo
pinzolo / Gemfile
Created June 25, 2014 01:04
失敗した後のGemfile
source 'https://rubygems.org'
gem 'rails'
# some gems
group :development, :test do
gem 'rspec'
gem 'rspec-rails'
gem 'pry'
source 'https://rubygems.org'
gem 'rails'
# some gems
group :development, :test do
gem 'rspec'
gem 'rspec-rails'