Rails 数据库表之间关系

一对一关系实例: one-to-one

Ruby代码
  1. class Order < ActiveRecord::Base   
  2. has_one :paid_order,   
  3.      :class_name =>"Order",   
  4.      :foreign_key => "order_id",   
  5.      :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)

Ruby代码
  1. class Invoice < ActiveRecord::Base   
  2. belongs_to :order  
class Invoice < ActiveRecord::Base
belongs_to :order



可选参数:class_name, :foreign_key,  order, :conditions

Ruby代码
  1. :dependent => true #删除主表行时同时删除子行   
  2. 自定义的order用法:   
  3. class Customer < ActiveRecord::Base   
  4. has_many :orders  
  5. has_one :most_recent_order,   
  6.      :class_name => 'Order',   
  7.      :order => 'created_at DESC'  
  8. end  
:dependent => true #删除主表行时同时删除子行
自定义的order用法:
class Customer < ActiveRecord::Base
has_many :orders
has_one :most_recent_order,
     :class_name => 'Order',
     :order => 'created_at DESC'
end



主.从 将被保存

Ruby代码
  1. an_invoice = Invoice.new(...)   
  2. order.invoice = an_invoice # invoice被保存  
an_invoice = Invoice.new(...)
order.invoice = an_invoice # invoice被保存


从.主 将要手动保存

新增加的方法:

Ruby代码
  1. product(force_reload=false)   
  2. product=(obj)   
  3. build_product(attributes={})   
  4. create_product(attributes={})  
product(force_reload=false)
product=(obj)
build_product(attributes={})
create_product(attributes={})


一对多关系(one-to-many)

Ruby代码
  1. class Order < ActiveRecord::Base   
  2. has_many :line_items  
class Order < ActiveRecord::Base
has_many :line_items


可选参数除了上面的,还有:exclusively_dependent, :finder_sql,:counter_sql
:exclusively_dependent 在子行没有其它表格瓜葛的情况下使用, 加快处理速度.
:finder_sql的使用实例:

Ruby代码
  1. class Order < ActiveRecord::Base   
  2. has_many :rails_line_items,   
  3.       :class_name => "LineItem",   
  4.       :finder_sql => "select l. * from line_items l, products p " +   
  5.       " where l.product_id = p.id " +   
  6.       " and p.title like '%rails%'"  
  7. 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的用法:

Ruby代码
  1. class Order < ActiveRecord::Base   
  2. has_many :line_items,   
  3.    :order =>"quantity, unit_price DESC"  
  4. end  
class Order < ActiveRecord::Base
has_many :line_items,
   :order =>"quantity, unit_price DESC"
end



主.从 将被保存

Ruby代码
  1. an_invoice = Invoice.new(...)   
  2. order.invoices <<an_invoice # invoice   
  3.   
  4. class LineItem < ActiveRecord::Base   
  5. belongs_to :order  
  6. . . .  
an_invoice = Invoice.new(...)
order.invoices <<an_invoice # invoice

class LineItem < ActiveRecord::Base
belongs_to :order
. . .



has_many后可以引用集合:

Ruby代码
  1. order = Order.find(123)   
  2. total = 0.0   
  3. order.line_items.each do |li|   
  4. total += li.quantity * li.unit_price   
  5. end  
order = Order.find(123)
total = 0.0
order.line_items.each do |li|
total += li.quantity * li.unit_price
end



新增加的方法:

Ruby代码
  1. orders(force_reload=false)   
  2. orders <<order   
  3. orders.push(order1, ...)   
  4. orders.delete(order1, ...)   
  5. orders.clear   
  6. orders.find(options...)   
  7. orders.build(attributes={})   
  8. 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):

Ruby代码
  1. class Product < ActiveRecord::Base   
  2. has_and_belongs_to_many :categories  
  3. #. . .   
  4. class Category < ActiveRecord::Base   
  5. has_and_belongs_to_many :products  
  6. #. . .  
class Product < ActiveRecord::Base
has_and_belongs_to_many :categories
#. . .
class Category < ActiveRecord::Base
has_and_belongs_to_many :products
#. . .



要创建一个中间表格:
categories_products
两表格名字复数形式相联, 排字母前后排序

表格内连关联键


预载子表
用:include将子表内容加入内存,提高查询速度, 但损耗内存:

Ruby代码
  1. for post in Post.find(:all:include => [:author:comments])   
  2. puts "Post: #{post.title}"  
  3. puts "Written by: #{post.author.name}"  
  4. puts "Last comment on: #{post.comments.first.created_on}"  
  5. 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加参数:

Ruby代码
  1. counter_cache => true  
counter_cache => true


数据库加 子表格名(复数)_count 段, 并加 :default=>0参数.
然后用 .size可以读取子表行数.
刷新数据读法:

Ruby代码
  1. product.line_items(:refresh).size  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值