【发布时间】:2010-03-30 16:03:42
【问题描述】:
喂,
我的代码:
@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
【问题讨论】:
标签: ruby-on-rails count limit has-many offset