The Six Types of Rails Association
翻译整理自:http://guides.rubyonrails.org/v3.2.13/association_basics.html
想吐槽一句,http://guides.ruby-china.org/ 翻译地太慢了!!!
csdn 复制过来格式都有误啊,改格式改了好几遍了。烦心。。。。
什么时候能支持markdown呢-。-
Rails支持以下6种关联类型:
- belongs_to
- has_one
- has_many
- has_many :through
- has_one :through
- has_and_belongs_to_many
class Order < ActiveRecord::Base
belongs_to :customer
end
class Supplier < ActiveRecord::Base
has_one :account
end
class Customer < ActiveRecord::Base
has_many :orders
end
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
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
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end
http://www.w3school.com.cn/sql/sql_foreignkey.asp
class Supplier < ActiveRecord::Base
has_one :account
end class Account < ActiveRecord::Base
belongs_to :supplier
end
class CreateSuppliers < ActiveRecord::Migration
def change
create_table :suppliers do |t|
t.string :name
t.timestamps
end create_table :accounts do |t|
t.integer :supplier_id
t.string :account_number
t.timestamps
end
end
end
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
http://railscasts.com/episodes/154-polymorphic-association
class Employee < ActiveRecord::Base
has_many :subordinates, :class_name => "Employee",
:foreign_key => "manager_id"
belongs_to :manager, :class_name => "Employee"
end
The Six Types of Rails Association的更多相关文章
- pcap文件的文件头的link type
http://www.tcpdump.org/linktypes.html Link-layer header type values LINKTYPE_ name LINKTYPE_ value C ...
- Unable to determine the principal end of an association between the types '***. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
MVC中数据库表如果是一对一的主键关系时要加[Required]不然会出错Unable to determine the principal end of an association between ...
- Linq------错误: Unable to determine the principal end of an association between the types
[Table("bma_stores")] public class Store { //加上即可 [Required] public virtual Product Produc ...
- rails enum用于存储数据
http://api.rubyonrails.org/classes/ActiveRecord/Enum.html 新的项目中有一个字段是展示类型,可以用下拉框去做,用string存储具体的类型字段. ...
- How to Test Controller Concerns in Rails 4
Concerns are a new feature that was added in Rails 4. They allow to clean up code in your models and ...
- Mybatis 高级结果映射 ResultMap Association Collection
在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...
- 10 steps to get Ruby on Rails running on Windows with IIS FastCGI- 摘自网络
Since the original tech preview release of FastCGI last year, we've been seeing a lot of requests fo ...
- Nginx + unicorn 运行多个Rails应用程序
PS:第一次写的很详细,可惜发布失败,然后全没了,这是第二次,表示只贴代码,剩下的自己领悟好了,这就是所谓的一鼓作气再而衰吧,希望没有第三次. 版本: ruby 2.1.0 rails 4.0.2 n ...
- Rails Migration Data Model栏位修改及数据类型介绍
测试版本Ruby:2.3.1 Rails:5.0.1 一.增加栏位 给devise默认的用户新增增加username字段 $ rails generate migration add_ ...
随机推荐
- [转] 小tips: 使用 等空格实现最小成本中文对齐 ---张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4562 一.重见天日第 ...
- php 函数 array_slice
array_slice array_slice -- 从数组中取出一段 <?php$input = array("a", ); // returns "c ...
- secureCRT使用小贴士
(一)使用WIN键盘 在securecrt界面:工具→键映射编辑器,在弹出的键盘中点击“home”,会弹出一个窗口,在“发送字符串”中输入:\033[1~ 另存为securecrt安装根目录下的Key ...
- MVC5 学习笔记1
新装了vs2013 开始试着学习MVC5 首先用了2013的内置的框架 这里提三点 1. bootstrap (现已加入mvc5豪华套餐) 他的框架已经加入了bootstrap 3.0的版本(http ...
- SqlHelper 帮助文档及详解--项目初步搭建
微软SqlHelper类中文注释和使用方法 相关链接: http://blog.csdn.net/itmaxin/article/details/7609566 SqlHelper.cs是N年前微软出 ...
- php的ob实现页面静态化
php页面静态化的原理,用最少的代码解释页面静态化 如何应用:在插入或更新数据到数据库时,就执行一下代码是一种比较好的方法.比如:php执行add()方法时(就是插入数据时) //开启缓存 Ob_st ...
- java Clone()克隆
转自:http://www.blogjava.net/orangelizq/archive/2007/10/17/153573.html 现在Clone已经不是一个新鲜词语了,伴随着“多莉”的产生这个 ...
- 窗体前端显示(ShowWindowAsync有许多优点)
H:=FindWindow('Tfrm_MainForm','aa'); if H>0 then begin ShowWindowAsync(h,SW_MAX); SetFore ...
- 14.6.4 Configuring the Memory Allocator for InnoDB 配置InnoDB 内存分配器
14.6.4 Configuring the Memory Allocator for InnoDB 配置InnoDB 内存分配器 当InnoDB 被开发时,内存分配提供了操作系统和 run-time ...
- VS2012中启动性能分析 独占样本数的分析
http://www.cnblogs.com/TianFang/archive/2012/11/18/2776266.html