$ rails g model User
belongs_to
has_one
require 'active_record' | |
ActiveRecord::Base.establish_connection :adapter => "sqlite3", | |
:database => "dbfile" | |
# Initialize the database schema | |
ActiveRecord::Base.connection.create_table :ducks do |t| | |
t.string :name | |
end | |
class Duck < ActiveRecord::Base |
require 'active_support' | |
require 'active_model' | |
# ... | |
module ActiveRecord | |
extend ActiveSupport::Autoload | |
autoload :Base | |
autoload :NoTouching | |
autoload :Persistence | |
autoload :QueryCache | |
autoload :Querying |
#-- | |
# Copyright (c) 2005-2015 David Heinemeier Hansson | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files (the | |
# "Software"), to deal in the Software without restriction, including | |
# without limitation the rights to use, copy, modify, merge, publish, | |
# distribute, sublicense, and/or sell copies of the Software, and to | |
# permit persons to whom the Software is furnished to do so, subject to | |
# the following conditions: |
require "active_support/inflector/methods" | |
module ActiveSupport | |
# Autoload and eager load conveniences for your library. | |
# | |
# This module allows you to define autoloads based on | |
# Rails conventions (i.e. no need to define the path | |
# it is automatically guessed based on the filename) | |
# and also define a set of constants that needs to be | |
# eager loaded: |
module ActiveRecord | |
class Base | |
extend ActiveModel::Naming | |
extend ActiveSupport::Benchmarkable | |
... | |
... | |
extend Translation | |
extend DynamicMatchers | |
extend Explain | |
extend Enum |
module ActiveRecord | |
# = Active Record Validations | |
# Active Record includes the majority of its validations from <tt>ActiveModel::Validations</tt> | |
# all of which accept the <tt>:on</tt> argument to define the context where the | |
# validations are active. Active Record will always supply either the context of | |
# <tt>:create</tt> or <tt>:update</tt> dependent on whether the model is a | |
# <tt>new_record?</tt>. | |
module Validations | |
extend ActiveSupport::Concern | |
include ActiveModel::Validations |
require 'active_model' | |
class User | |
include ActiveModel::Validations | |
attr_accessor :password | |
validate do | |
errors.add(:base, "Don't let dad choose the password.") if password == '1234' | |
end | |
end | |
module ActiveRecord | |
module Validations | |
#... | |
def self.included(base) | |
base.extend ClassMethods | |
#... | |
end | |
module ClassMethods | |
def validates_length_of(*attrs)#... |
module SecondLevelModule | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
def second_level_instance_method; 'ok'; end | |
module ClassMethods | |
def second_level_class_method; 'ok'; end | |
end |