我有这样的产品模型:
class Product < ActiveRecord::Base
has_many :images, :class_name => 'ProductImage', :order => 'position DESC', :dependent => :destroy
def image_thumb
images.first.image.thumb.url
end
def image
images.first.image.url
end
end
产品图片型号:
class ProductImage < ActiveRecord::Base
attr_accessible :image, :position, :product_id, :title
belongs_to :product
default_scope order('position ASC')
mount_uploader :image, ProductImageUploader
end
上传器型号:
class ProductImageUploader < CarrierWave::Uploader::Base
...
def default_url
asset_path([version_name, "default.jpg"].compact.join('_'))
end
end
但是,如果我不为产品上传任何图片,我将在 image_thumb 和 image 方法中得到“nil”。如果没有上传一张图片并且 Product 和 ProductImage 模型之间的关系是一对多的,如何获取 default_url?