一对一关系实例: one-to-one
- class Order < ActiveRecord::Base
- has_one :paid_order,
- :class_name =>"Order",
- :foreign_key => "order_id",
- :conditions => "paid_on is not null"
class Order < ActiveRecord::Base
has_one :paid_order,
:class_name =>"Order",
:foreign_key => "order_id",
:conditions => "paid_on is not null"
在表格上加order_id (表格名单数_id)
- class Invoice < ActiveRecord::Base
- belongs_to :order
class Invoice < ActiveRecord::Base
belongs_to :order
可选参数:class_name, :foreign_key, order, :conditions
- :dependent => true #删除主表行时同时删除子行
- 自定义的order用法:
- class Customer < ActiveRecord::Base
- has_many :orders
- has_one :most_recent_order,
- :class_name => 'Order',
- :order => 'created_at DESC'
- end
:dependent => true #删除主表行时同时删除子行
自定义的order用法:
class Customer < ActiveRecord::Base
has_many :orders
has_one :most_recent_order,
:class_name => 'Order',
:order => 'created_at DESC'
end
主.从 将被保存
- an_invoice = Invoice.new(...)
- order.invoice = an_invoice # invoice被保存
an_invoice = Invoice.new(...)
order.invoice = an_invoice # invoice被保存
从.主 将要手动保存
新增加的方法:
- product(force_reload=false)
- product=(obj)
- build_product(attributes={})
- create_product(attributes={})
product(force_reload=false)
product=(obj)
build_product(attributes={})
create_product(attributes={})
一对多关系(one-to-many)
- class Order < ActiveRecord::Base
- has_many :line_items
class Order < ActiveRecord::Base
has_many :line_items
可选参数除了上面的,还有:exclusively_dependent, :finder_sql,:counter_sql
:exclusively_dependent 在子行没有其它表格瓜葛的情况下使用, 加快处理速度.
:finder_sql的使用实例:
- class Order < ActiveRecord::Base
- has_many :rails_line_items,
- :class_name => "LineItem",
- :finder_sql => "select l. * from line_items l, products p " +
- " where l.product_id = p.id " +
- " and p.title like '%rails%'"
- end
class Order < ActiveRecord::Base
has_many :rails_line_items,
:class_name => "LineItem",
:finder_sql => "select l. * from line_items l, products p " +
" where l.product_id = p.id " +
" and p.title like '%rails%'"
end
order的用法:
- class Order < ActiveRecord::Base
- has_many :line_items,
- :order =>"quantity, unit_price DESC"
- end
class Order < ActiveRecord::Base
has_many :line_items,
:order =>"quantity, unit_price DESC"
end
主.从 将被保存
- an_invoice = Invoice.new(...)
- order.invoices <<an_invoice # invoice
- class LineItem < ActiveRecord::Base
- belongs_to :order
- . . .
an_invoice = Invoice.new(...)
order.invoices <<an_invoice # invoice
class LineItem < ActiveRecord::Base
belongs_to :order
. . .
has_many后可以引用集合:
- order = Order.find(123)
- total = 0.0
- order.line_items.each do |li|
- total += li.quantity * li.unit_price
- end
order = Order.find(123)
total = 0.0
order.line_items.each do |li|
total += li.quantity * li.unit_price
end
新增加的方法:
- orders(force_reload=false)
- orders <<order
- orders.push(order1, ...)
- orders.delete(order1, ...)
- orders.clear
- orders.find(options...)
- orders.build(attributes={})
- orders.create(attributes={})
orders(force_reload=false)
orders <<order
orders.push(order1, ...)
orders.delete(order1, ...)
orders.clear
orders.find(options...)
orders.build(attributes={})
orders.create(attributes={})
多对多关系(many-to-many):
- class Product < ActiveRecord::Base
- has_and_belongs_to_many :categories
- #. . .
- class Category < ActiveRecord::Base
- has_and_belongs_to_many :products
- #. . .
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
#. . .
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
#. . .
要创建一个中间表格:
categories_products
两表格名字复数形式相联, 排字母前后排序
表格内连关联键
略
预载子表
用:include将子表内容加入内存,提高查询速度, 但损耗内存:
- for post in Post.find(:all, :include => [:author, :comments])
- puts "Post: #{post.title}"
- puts "Written by: #{post.author.name}"
- puts "Last comment on: #{post.comments.first.created_on}"
- end
for post in Post.find(:all, :include => [:author, :comments])
puts "Post: #{post.title}"
puts "Written by: #{post.author.name}"
puts "Last comment on: #{post.comments.first.created_on}"
end
自动计算子表行数
belongs_to加参数:
- counter_cache => true
counter_cache => true
数据库加 子表格名(复数)_count 段, 并加 :default=>0参数.
然后用 .size可以读取子表行数.
刷新数据读法:
- product.line_items(:refresh).size