$ rails g model user email age:integer
- data_type
- integer
- primary_key
########################################################## | |
# polymorphic as module | |
########################################################## | |
# app/models/comment.rb | |
# | |
class Comment < ActiveRecord::Base | |
belongs_to :commenter, :polymorphic => true | |
end |
# 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 |
# rails migration | |
######################## | |
# add column to exsting table | |
######################## | |
def up | |
add_column :users, :status, :string | |
User.find_each do |user| | |
user.status = 'active' |
# rails methods | |
# returns column_names arrays | |
Model.column_names | |
# returns attr_keys arrays | |
object.attributes.keys | |
# returns attr_keys values | |
object.attributes.values |
reference:
Part1: astockwell.com
Part2: astockwell.com
reference:
Part1: astockwell.com
Part2: astockwell.com
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.
Lets say we have a model Computer