嗨,
我的代码:
@profile.images
我想一次只得到 10 张图像,偏移量为 10,就像这样
@profile.images(:limit => 10, :offset => 10)
而不是这样
has_many :images, :limit => 10, :offset => 10
然后我想以某种方式计算该配置文件的所有图像。
@profile.count_images
谢谢 (:
has_many :images, :foreign_key => 'on_id', :conditions => 'on_type = "profile"' do
def paginate(page = 1, limit = 10, offset = nil)
page = nil if page < 1
limit = 1 if limit < 1
offset = 0 if(offset && offset < 0)
offset = 0 if (!page)
offset = limit * (page - 1) if (page)
all(:limit=> limit, :offset => offset)
end
结尾
现在我想将此行为添加到其他 has_many 关系中。但我不想复制粘贴代码......知道吗?:P