翻译整理自: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
1. belongs_to :在两个model间建立一对一的关系
 
class Order < ActiveRecord::Base
belongs_to :customer
end
意味着每一个order能且只能被赋给一个customer。
2. has_one :同样在两个model间建立一对一的关系,但语义和结果有点区别。
意味着一个model的每个实例含有或拥有另一个model的一个实例。
class Supplier < ActiveRecord::Base
has_one :account
end
个人理解:有点像是和belongs_to相反的关系,一个是属于,一个是拥有。
3. has_many :和另一个模型有着一对多的关系,经常和belongs_to成对出现。
意味着一个模型每个实例有0个或多个另一模型的实例。
class Customer < ActiveRecord::Base
has_many :orders
end
注意: 此处order为复数形式。
4. has_many :through 经常被用来和另一个模型建立多对多的关系。
原model可以通过(through)第三方model和0个或多个另一model的实例进行匹配。
假想一个病人看病的例子,病人需要预约医生才可以看病,关系可被定义为:
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
5. has_one :through  一对一的关系,与上一个类似,原model可以通过第三方model和1个另一model的实例相匹配。
举例:每个供应商有一个账号,每个账户有一个相关联的账号历史,关系定义如下:
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
6. has_and_belongs_to_many :直接和另一个model建立多对多的关系,不再通过中间model。
例如,每个配件有很多个部分,每个部分出现在很多个配件中,定义如下:
class Assembly < ActiveRecord::Base
has_and_belongs_to_many :parts
end class Part < ActiveRecord::Base
has_and_belongs_to_many :assemblies
end

到底该选用哪一种关系呢???
1. 该选用 has_one 还是 belongs_to ?
区别在于你在哪里放置了foreign key
ps : foreign key(外键):父数据表(Parent Entity)的主键(primary key)会放在另一个数据表,当做属性以创建彼此的关系,而这个属性就是外键。参考:
http://www.w3school.com.cn/sql/sql_foreignkey.asp
如:Orders表中的customer_id就是Orders表的外键,而order_id是主键。
举例说明:Supplier 和 account 的关系
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
一看就知道 supplier_id 是accouts表的外键,所以,关系定义确实是对的。

2. 该选用 has_many :through 还是 has_and_belongs_to_many ?
它们其实是Rails提供的两种声明多对多关系的方法。后者更简单些,它允许你直接定义多对多关系。

例子见上面,如果你需要将关系模型作为一个独立的数据表而且不需要对该关系模型做任何处理的话,使用
has_and_many是比较简单的,尽管你还要在数据库中建立一个连接表(joining table),比如 assemblies_parts表。

但是如果在连接模型中你需要数据验证(validations),回调函数(callbacks),或者其他参数时,你就应该使用has_many :through这种方式了。

3. 多态关联(polymorphic association)
A model can belong to more than one other model, on a single association.
For example, 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

4. 自我连接(self joins)

有时候你需要一个model,它的内部之间也有联系。
比如你想把所有的雇员都存放在一个单独的数据库模型中,但它同时又能跟踪经理和下属的关系。
可以这样定义:
class Employee < ActiveRecord::Base
has_many :subordinates, :class_name => "Employee",
:foreign_key => "manager_id"
belongs_to :manager, :class_name => "Employee"
end

这样一来,你就可以获取 @employee.subordinates 和 @employee.manager 了。


不想写地太长,所以只摘取了其中部分,另外能力有限,有些部分没能翻译。
今天在大神的点拨下基本完成了数据关系的设计, 嘿嘿, 感谢HalFtaN, 开开心心做国创去咯 ^_^

欢迎各位指正 :)

The Six Types of Rails Association的更多相关文章

  1. pcap文件的文件头的link type

    http://www.tcpdump.org/linktypes.html Link-layer header type values LINKTYPE_ name LINKTYPE_ value C ...

  2. 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 ...

  3. Linq------错误: Unable to determine the principal end of an association between the types

    [Table("bma_stores")] public class Store { //加上即可 [Required] public virtual Product Produc ...

  4. rails enum用于存储数据

    http://api.rubyonrails.org/classes/ActiveRecord/Enum.html 新的项目中有一个字段是展示类型,可以用下拉框去做,用string存储具体的类型字段. ...

  5. 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 ...

  6. Mybatis 高级结果映射 ResultMap Association Collection

    在阅读本文章时,先说几个mybatis中容易混淆的地方: 1. mybatis中的列不是数据库里的列而是查询里的列,可以是别名(如 select user_name as userName,这时col ...

  7. 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 ...

  8. Nginx + unicorn 运行多个Rails应用程序

    PS:第一次写的很详细,可惜发布失败,然后全没了,这是第二次,表示只贴代码,剩下的自己领悟好了,这就是所谓的一鼓作气再而衰吧,希望没有第三次. 版本: ruby 2.1.0 rails 4.0.2 n ...

  9. Rails Migration Data Model栏位修改及数据类型介绍

    测试版本Ruby:2.3.1   Rails:5.0.1 一.增加栏位       给devise默认的用户新增增加username字段 $ rails generate migration add_ ...

随机推荐

  1. Oracle中Long类型的使用与不可使用

    ORA-01754 表只能包含一个LONG类型的列alter table 表名 add 字段名 long raw错误原因:数据表中同时建立了LONG RAW类型和LONG类型.------------ ...

  2. PreResultListener使用

    PreResultListener是一个监听器接口,可以在Action处理完之后,系统转入实际视图前被回调. Struts2应用可以给Action.拦截器添加PreResultListener监听器, ...

  3. 浅析tornado web框架

    tornado简介 1.tornado概述 Tornado就是我们在 FriendFeed 的 Web 服务器及其常用工具的开源版本.Tornado 和现在的主流 Web 服务器框架(包括大多数 Py ...

  4. js 图片base64

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  5. AngularJS: 自定义指令与控制器数据交互

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 直接将视频文件原码流转换成YUV,输出到屏幕显示

    #include "stdafx.h" #define inline _inline#ifndef INT64_C#define INT64_C(c) (c ## LL)#defi ...

  7. quartz定时任务时间配置

    quartz定时任务时间设置描述(2011-03-03 16:23:50)转载▼标签: quartz时间it 分类: 凌乱小记  这些星号由左到右按顺序代表 :     *    *     *    ...

  8. HTTP之Content-Length

    在HTTP协议中,有Content-Length的详细解读.Content-Length用于描述HTTP消息实体的传输长度the transfer-length of the message-body ...

  9. http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html

    http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html T ...

  10. bzoj3209

    首先这道题目不难想到将答案转化为这种形式 2^s[2]*3*s[3]*…max*s[max] 这时候我们要分类讨论,设n的二进制位数为t 当1~n中二进制位数小于t时 我们可以直接用组合的知识,二进制 ...