- 浏览: 2548609 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Rails Study(9)Associations First Part
1. Why Associations?
Without Associations, we will configure and use the models like this:
class Customer < ActiveRecord::Base
end
class Order < ActiveRecord::Base
end
When we want to create an order
@order = Order.create(:order_date => Time.now, :customer_id => @customer.id)
consider deleting a customer
@orders = Order.find_all_by_customer_id(@customer.id)
@orders.each do |order|
order.destroy
end
@customer.destroy
With association.
class Customer < ActiveRecord::Base
has_manyrders, :dependent => :destroy
end
class Order < ActiveRecord::Base
belongs_to :customer
end
@order = @customer.orders.create(:order_date => Time.now)
@customer.destroy
2. The Types of Associations
belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many
2.1 The belongs_to Association
Each order can be assigned to exactly one customer.
class Order < ActiveRecord::Base
belongs_to :customer
end
2.2 The has_one Association
one supplier has only one account.
class Supplier < ActiveRecord::Base
has_one :account
end
2.3 The has_many Association
You'll often find this association on the "other side" of a belongs_to association.
Customers have orders or not.
class Customer < ActiveRecord::Base
has_manyrders
end
2.4 The has_many :through Association
Consider a medical practice where patients make appointments to see physicians.
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
If a document has many sections, and a section has many paragraphs.
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
2.5 The has_one :through Association
Each supplier has one account, each account is associated with one account history.
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
2.6 The has_and_belongs_to_many Association
Application includes assemblies and parts, each assembly having many parts and each part appearing in many assembilies.
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
assemblies ------> assemblies_parts <----------- parts
id assembliy_id id
name part_id part_number
2.7 Choosing Between belongs_to and has_one
It makes more sense to say that a supplier owns an account than that an account owns a supplier.
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
2.8 Choosing Between has_many :through and has_and_belongs_to_many
The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the
relationship model as an independent entity. If there is no need for relationship model, it may be simpler to set up
a has_and_belongs_to_many relationship.
2.9 Polymorphic Associations
You might have a picture model that belongs to either an employee model or a product model.
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
employees -------> pictures <---------------------products
id id id
name name name
imageable_id
imageable_type
The migration class can be:
class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
end
end
class CreatePicture < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.string :name
t.references :imageable, :polymorphic => true
t.timestamps
end
end
end
2.10 Self Joins
Relationships such as between manager and subordinates.
class Employee < ActiveRecord::Base
has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id"
belongs_to :manager, :class_name => "Employee"
end
retrieve @employee.subordinates @employee.manager
3 Tips, Tricks, and Warnings
3.1 Controlling Caching
customer.orders #retrieves orders from the db
customer.orders.size #uses the cached copy of orders
customer.orders.empty? #uses the cached copy of orders
customer.orders(true).empty? #discards the cached, goes back to the db
3.2 Avoiding Name Collisions
attributes or connection are bad names for associations.
3.3 Updating the Schema
3.3.1 Creating Foreign Keys for belongs_to Associations
class Order < ActiveRecord::Base
belongs_to :customer
end
class CreateOrders < ActiveRecord::Migration
def self.up
create_tablerders do |t|
t.datetimerder_date
t.string rder_number
t.integer :customer_id
end
end
end
3.3.2 Creating Join Tables for has_and_belongs_to_many Associations
Creating the assemblies_parts table.
class CreateAssemblyPartJoinTable < ActiveRecord::Migration
def self.up
create_table :assemblies_parts, :id => false do |t|
t.integer :assembly_id
t.integer :part_id
end
end
end
3.4 Controlling Association Scope
classes in the same module
module MyApplication
module Business
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
end
end
module MyApplication
module Business
class Supplier < ActiveRecord::Base
has_one :account, :class_name => "MyApplication::Billing::Account"
end
end
module Billing
class Account < ActiveRecord::Base
belongs_to :supplier, :class_name => "MyApplication::Business::Supplier"
end
end
end
4. snip..
references:
http://guides.rubyonrails.org/association_basics.html
1. Why Associations?
Without Associations, we will configure and use the models like this:
class Customer < ActiveRecord::Base
end
class Order < ActiveRecord::Base
end
When we want to create an order
@order = Order.create(:order_date => Time.now, :customer_id => @customer.id)
consider deleting a customer
@orders = Order.find_all_by_customer_id(@customer.id)
@orders.each do |order|
order.destroy
end
@customer.destroy
With association.
class Customer < ActiveRecord::Base
has_manyrders, :dependent => :destroy
end
class Order < ActiveRecord::Base
belongs_to :customer
end
@order = @customer.orders.create(:order_date => Time.now)
@customer.destroy
2. The Types of Associations
belongs_to
has_one
has_many
has_many :through
has_one :through
has_and_belongs_to_many
2.1 The belongs_to Association
Each order can be assigned to exactly one customer.
class Order < ActiveRecord::Base
belongs_to :customer
end
2.2 The has_one Association
one supplier has only one account.
class Supplier < ActiveRecord::Base
has_one :account
end
2.3 The has_many Association
You'll often find this association on the "other side" of a belongs_to association.
Customers have orders or not.
class Customer < ActiveRecord::Base
has_manyrders
end
2.4 The has_many :through Association
Consider a medical practice where patients make appointments to see physicians.
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
end
If a document has many sections, and a section has many paragraphs.
class Document < ActiveRecord::Base
has_many :sections
has_many :paragraphs, :through => :sections
end
class Section < ActiveRecord::Base
belongs_to :document
has_many :paragraphs
end
class Paragraph < ActiveRecord::Base
belongs_to :section
end
2.5 The has_one :through Association
Each supplier has one account, each account is associated with one account history.
class Supplier < ActiveRecord::Base
has_one :account
has_one :account_history, :through => :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
has_one :account_history
end
class AccountHistory < ActiveRecord::Base
belongs_to :account
end
2.6 The has_and_belongs_to_many Association
Application includes assemblies and parts, each assembly having many parts and each part appearing in many assembilies.
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end
class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
assemblies ------> assemblies_parts <----------- parts
id assembliy_id id
name part_id part_number
2.7 Choosing Between belongs_to and has_one
It makes more sense to say that a supplier owns an account than that an account owns a supplier.
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
2.8 Choosing Between has_many :through and has_and_belongs_to_many
The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the
relationship model as an independent entity. If there is no need for relationship model, it may be simpler to set up
a has_and_belongs_to_many relationship.
2.9 Polymorphic Associations
You might have a picture model that belongs to either an employee model or a product model.
class Picture < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
end
class Employee < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
class Product < ActiveRecord::Base
has_many :pictures, :as => :imageable
end
employees -------> pictures <---------------------products
id id id
name name name
imageable_id
imageable_type
The migration class can be:
class CreatePictures < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.string :name
t.integer :imageable_id
t.string :imageable_type
t.timestamps
end
end
end
class CreatePicture < ActiveRecord::Migration
def self.up
create_table :pictures do |t|
t.string :name
t.references :imageable, :polymorphic => true
t.timestamps
end
end
end
2.10 Self Joins
Relationships such as between manager and subordinates.
class Employee < ActiveRecord::Base
has_many :subordinates, :class_name => "Employee", :foreign_key => "manager_id"
belongs_to :manager, :class_name => "Employee"
end
retrieve @employee.subordinates @employee.manager
3 Tips, Tricks, and Warnings
3.1 Controlling Caching
customer.orders #retrieves orders from the db
customer.orders.size #uses the cached copy of orders
customer.orders.empty? #uses the cached copy of orders
customer.orders(true).empty? #discards the cached, goes back to the db
3.2 Avoiding Name Collisions
attributes or connection are bad names for associations.
3.3 Updating the Schema
3.3.1 Creating Foreign Keys for belongs_to Associations
class Order < ActiveRecord::Base
belongs_to :customer
end
class CreateOrders < ActiveRecord::Migration
def self.up
create_tablerders do |t|
t.datetimerder_date
t.string rder_number
t.integer :customer_id
end
end
end
3.3.2 Creating Join Tables for has_and_belongs_to_many Associations
Creating the assemblies_parts table.
class CreateAssemblyPartJoinTable < ActiveRecord::Migration
def self.up
create_table :assemblies_parts, :id => false do |t|
t.integer :assembly_id
t.integer :part_id
end
end
end
3.4 Controlling Association Scope
classes in the same module
module MyApplication
module Business
class Supplier < ActiveRecord::Base
has_one :account
end
class Account < ActiveRecord::Base
belongs_to :supplier
end
end
end
module MyApplication
module Business
class Supplier < ActiveRecord::Base
has_one :account, :class_name => "MyApplication::Billing::Account"
end
end
module Billing
class Account < ActiveRecord::Base
belongs_to :supplier, :class_name => "MyApplication::Business::Supplier"
end
end
end
4. snip..
references:
http://guides.rubyonrails.org/association_basics.html
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 471NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 331Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 431Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 381Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 472NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 421Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 336Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 246GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 325GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 312Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 291Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 309Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 287NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 572NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 263Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 369Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
《Head First Rails中文版.2011.12》这本书是针对初学者设计的一本深入浅出的Rails教程,旨在帮助读者快速掌握Ruby on Rails框架。Ruby on Rails(简称Rails)是由David Heinemeier Hansson开发的一个开源Web应用...
《Head First Rails》是为学习Ruby on Rails的学习者提供的一个伴侣手册。本书是Head First系列的一部分,该系列书籍以其结合实际应用场景的教育方式而著称,旨在帮助读者快速掌握技能并迅速上手。《Head First ...
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part14
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part03.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part07.rar
征服RUBY ON RAILS WEB开发技术详解电子版,与part1一同解压
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part08.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part06.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part01.rar
你将学习一切Rails scaffolding的基本原理,以创建自定义的交互式网络应用程序,全部使用Rails的一套丰富的工具和MVC框架。 你将掌握数据库交互、Ajax和XML的集成、丰富的内容,甚至数据的动态图形——曾经要使用...
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part09.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part13.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part12.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part10.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part01.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part05.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part04.rar
(新[共14])Web开发敏捷之道--应用Rails进行敏捷Web开part11.rar
英文版,文字版, 带目录。
ruby on rails web开发详解的电子版,要用UnicornViewer打开才行。