Rails 数据库表之间关系

来源:互联网 发布:java程序员的自我修养 编辑:程序博客网 时间:2024/03/29 09:42

一对一关系实例: 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"  


在表格上加order_id (表格名单数_id)

Ruby代码
  1. class Invoice < ActiveRecord::Base   
  2. 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  



主.从 将被保存

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


从.主 将要手动保存

新增加的方法:

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


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

Ruby代码
  1. class Order < ActiveRecord::Base   
  2. 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  


order的用法:

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



主.从 将被保存

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



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  



新增加的方法:

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={})  



多对多关系(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. #. . .  



要创建一个中间表格:
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  



自动计算子表行数
belongs_to加参数:

Ruby代码
  1. counter_cache => true  


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

Ruby代码
  1. product.line_items(:refresh).size  
原创粉丝点击