诗歌rails之有嵌套关系的model has_one

Ruby代码
  1. class Book < ActiveRecord::Base  
  2.  has_one :distributor  
  3. end  
  4.   
  5. class Distributor < ActiveRecord::Base  
  6.  belongs_to :book  
  7.  has_many :agents  
  8. end  
  9.   
  10. class Agent < ActiveRecord::Base  
  11.  belongs_to :distributor  
  12.  has_many :shops  
  13. end  
  14.   
  15. class Shop < ActiveRecord::Base  
  16.  belongs_to :agent  
  17. end  
  18.   
  19. #Schema - http://pastie.org/426261  
  20.   
  21. def test_should_load_all_shops  
  22.   shop_1= Shop.create!  
  23.   shop_2= Shop.create!  
  24.   book= Book.create!(:distributor => Distributor.create!(:agents=> [Agent.create!(:shops => [shop_1, shop_2])]))  
  25.   
  26.   
  27.   loaded_version = Book.find(book.id, :include => [:distributor => {:agents => :shops}], :order => 'shops.id')  
  28.   
  29.   assert(loaded_version.distributor.agents.first.shops.size == 2)  
  30.   #THIS ASSERTION FAILS WITH SHOPS.SIZE BEING 1, INSTEAD OF 2  
  31. end  
posted @ 2009-08-24 18:17  麦飞  阅读(243)  评论(0编辑  收藏  举报