ROR的不同实现

我正在制作一个非常简单的ROR网站。

class Product  'Photo', :order => 'name' has_one :random_photo_1, :class_name => 'Photo', :order => 'RAND()' def random_photo_2 Photo.find(:first, :conditions => { :product_id => self.id }, :order => 'RAND()') end end 

在实现许多类ActiveRecord期间,我怀疑,我不明白random_photo_1实现random_photo_2方法之间的区别。

PS我很抱歉我的英语。

他们都会做同样的工作。

好处:random_photo_1是你可以轻松地加载所有“随机照片”关联,无论何时你找到几个产品,如果你要展示很多产品和随机照片,这将真正有助于性能在你的观点。

 #:include will eagerly load the associations @products = Product.find(:all, :include => :random_photo_1) 

然后,每当您在视图上迭代@products时,如果您这样做:

 @products.each do |product| #This will not do a new select against the database <%= product.random_photo_1 %> end