Skip to content

Instantly share code, notes, and snippets.

@oojikoo-gist
oojikoo-gist / polymorphic_module.rb
Last active August 29, 2015 14:19
rails: polymorphic module
##########################################################
# polymorphic as module
##########################################################
# app/models/comment.rb
#
class Comment < ActiveRecord::Base
belongs_to :commenter, :polymorphic => true
end
@oojikoo-gist
oojikoo-gist / generator_dictionary.md
Last active August 29, 2015 14:19
rails: generator dictionary

rails generator dictionary

basic

$ rails g model user email age:integer
  • data_type
    • integer
    • primary_key
@oojikoo-gist
oojikoo-gist / meta_programming_dictionary.rb
Last active August 29, 2015 14:19
rails: meta programming dictionary
# ref: http://www.jimmycuadra.com/posts/metaprogramming-ruby-class-eval-and-instance-eval)
class Person
end
Person.class_eval do
def say_hello
"Hello!"
end
end
@oojikoo-gist
oojikoo-gist / migration_dictionary.rb
Created April 20, 2015 18:01
rails: migration dictionary
# rails migration
########################
# add column to exsting table
########################
def up
add_column :users, :status, :string
User.find_each do |user|
user.status = 'active'
@oojikoo-gist
oojikoo-gist / rails_dictionary.rb
Last active August 29, 2015 14:19
rails: methods dictionary
# rails methods
# returns column_names arrays
Model.column_names
# returns attr_keys arrays
object.attributes.keys
# returns attr_keys values
object.attributes.values
@oojikoo-gist
oojikoo-gist / alternative_polymorphic.md
Created April 20, 2015 13:34
rails: polymorphic model alternative

Alternative polymorphic model

Here's a fully working example:

# The migration file:
class CreateUserEntities < ActiveRecord::Migration
  def change
    create_table :user_entities do |t|
      t.integer :user_id
@oojikoo-gist
oojikoo-gist / rspec3_config.md
Created April 17, 2015 20:25
rails: rspec3

Rails 4 with Rspec 3

Configure your Gemfile

# Gemfile
group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
  gem 'capybara'
@oojikoo-gist
oojikoo-gist / Polymorphic_with_devise_2.md
Created April 17, 2015 13:51
rails: Polymorphic Associations in Rails4, Part2
@oojikoo-gist
oojikoo-gist / Polymorphic_devise.md
Last active August 29, 2015 14:19
rails: Polymorphic Associations in Rails4

Polymorphic Associations in Rails 4

Adding a Polymorphic Association to Devise User Accounts

reference:

Part1: astockwell.com

Part2: astockwell.com

PROBLEM

@oojikoo-gist
oojikoo-gist / STI.md
Created April 17, 2015 03:20
rails: STI(Single Table Inheritance)

Single Table inheritance

reference: blog.thirst.co

In a nutshell, STI allows you to create subclasses of a particular database table. Using a single table, you can cast rows to specific objects that extend the base model.

how to create STI relationships in Rails

Lets say we have a model Computer