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_ ...
随机推荐
- CoffeeScript飞一样的写javascript
之前看到同事在使用coffeescript写js,当我看到那简介的coffee文件,就深深的被coffescript吸引了,简洁的语法,熟练之后会大大提升javascript的开发速度,写脚本也能像飞 ...
- jquery 三种开始写法
在 jquery 代码中使用 $(document).ready()时,位于其中的所有代码都会在 DOM 加载后立即执行 第一种(推荐)$(document).ready(function(){ ...
- js 中特殊形势的函数-匿名函数的应用
javascript中的匿名函数,那什么叫做匿名函数? 匿名函数就是没有函数名称:演示代码: <script> function(x,y){ return x+y //这个就是一个匿名函数 ...
- andriod 开发记录apidemos 错误解决
android sdk 里面有simple 文件夹里面有对应的demo 但是拿出来esplise运行报错 解决方案如下 右键错误代码goto,给对应错误的单引号前加 \ 原文http://stack ...
- 学习Swift -- 协议(上)
协议(上) 协议是Swift非常重要的部分,协议规定了用来实现某一特定工作或者功能所必需的方法和属性.类,结构体或枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能够满足协议要求 ...
- Struts2技术内幕-----第七章
1)基于人机交互的请求--响应模式主要由哪三大要素构成? ①沟通协议-----人和机器都能够明白的数据通信格式 ②请求内容-----人通过某种机制向机器发起的数据请求 ③响应内 ...
- 面向对象js瀑布流效果
index.html <!doctype html><html lang="en"> <head> <!--网站编码格式,UTF-8 国 ...
- Java基础——异常机制
[捕获异常] 硬件的错误.输入错误.物理限制等问题,都可能导致程序运行时的异常出现. 1.异常的分类层次 在java中,异常对象都是由Throwable类继承而来的,主要分为两大类: Error和Ex ...
- 多线程操作中为什么使用while而不是if来做判断状态是否就绪
在多线程操作中,我们常常会遇到需要先判断信号量状态是否就绪,然后执行后续操作的场景.这里对状态的判断使用的是while而不是单线程下常用的if. 以下示例展示了一个简单的生产者-消费者模型:当队列满的 ...
- Spring MVC 解读——<context:component-scan/>
转自:http://my.oschina.net/HeliosFly/blog/203149 作者:GoodLoser. Spring MVC 解读---<context:component-s ...