作者:Ryan Daigle 来源:JavaEye   酷勤网收集 2008-05-29

摘要
  rails2.1中新特性的文章.这里是这个系列中的一篇. Has one :through,has_one终于长得和has_many一样高了,它也支持了:through选项. 并且当through属性改变后,中间属性会随之改变.当through连接有多个record,has_one会返回第一个.

系列文章汇总《Ruby on Rails 2.1新特性

Ryan Daigle发布了一系列关于rails2.1中新特性的文章.这里是这个系列中的一篇.

Has one :through

has_one终于长得和has_many一样高了,它也支持了:through选项.

Ruby代码 复制代码
  1. class Magazine < ActiveRecord::Base   
  2.   has_many :subscriptions  
  3. end  
  4.   
  5. class Subscription < ActiveRecord::Base   
  6.   belongs_to :magazine  
  7.   belongs_to :user  
  8. end  
  9.   
  10. class User < ActiveRecord::Base   
  11.   has_many :subscriptions  
  12.   has_one :magazine:through => : subscriptions, :conditions => ['subscriptions.active = ?'true]   
  13. end  

并且当through属性改变后,中间属性会随之改变
Ruby代码 复制代码
  1. @ryan.subscriptions #=> []   
  2. @ryan.magazine = Magazine.create(:name => 'Hustler')   
  3. @ryan.subscriptions #=> [<Subscription magazine_id: 1, user_id: 1 ...>]  


译者注: 当through连接有多个record,has_one会返回第一个.


原文:http://ryandaigle.com/
来自:http://www.javaeye.com/news/2320

分类: .NET技术 网页设计 交互设计

上一篇:客户-服务器计算:未来的Web?   下一篇:Ruby on Rails 2.1新特性