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_ ...
随机推荐
- Bootstrap_排版_表格
一.基础表格 <table class="table"> <thead> <tr> <th>表格标题</th> < ...
- 局部视图(partial)
局部视图(partial) 原文:Partial Views作者:Steve Smith翻译:张海龙(jiechen).刘怡(AlexLEWIS)校对:许登洋(Seay).何镇汐.魏美娟(初见) AS ...
- 拦截QT关闭窗口的CloseEvent
QDialog类下有一个虚函数 void QDialog::closeEvent ( QCloseEvent * e ) [virtual protected] 通过实现closeEve ...
- 初级ant的学习
一.安装ant 到官方主页http://ant.apache.org下载新版(目前为Ant1.8.1)的ant,得到的是一个apache-ant-1.8.1-bin.zip的压缩包.将其解压到你的硬盘 ...
- NWERC 2012 Problem I Idol
又是个2-sat的模板题: 反正评委的选择必须有一个是正确的,1错误,那么2就必须正确: 这就是一个2-sat问题. 直接上白书的模板啊,不过稍微要注意的一点是对于第一个点必须要选择,不然就违反了题意 ...
- 如何测试 Android 中的定时事件
测试定时事件不太容易,比如要测试 AlarmManager 中定时明天4点的一个事件,你总不能等到明天4点再看看吧. Roman Nurik 提供了两个用来测试定时事件的命令:adb shell du ...
- Android与服务器端数据交互(http协议整合struts2+android)
在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...
- Reverse Linked List 递归非递归实现
单链表反转--递归非递归实现 Java接口: ListNode reverseList(ListNode head) 非递归的实现 有2种,参考 头结点插入法 就地反转 递归的实现 1) Divide ...
- 查看SGA和PGA使用率
select name,total,round(total-free,2) used, round(free,2) free,round((total-free)/total*100,2) pctus ...
- TCP/IP FTP/TFTP
引言 从一台计算机向另一台计算机传送文件是在连网或互联网环境中最常见的任务.而FTP和TFTP就是这样的协议. 关于文件传输协议FTP? 端口21使用服务TCP [FTP模型] FTP连接? 1.控制 ...