我有一个 has_many 关系,我想设置自定义限制和偏移量。以及计算它们

发布于 2024-08-27 11:21:57 字数 824 浏览 10 评论 0 原文

嘿,

我的代码:

@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

Hy,

My code:

@profile.images

and i would like to get only 10 images at time and with a 10 offset, like this

@profile.images(:limit => 10, :offset => 10)

and not like this

has_many :images, :limit => 10, :offset => 10

Then I would like to count in someway all the images for that profile.

@profile.count_images

Thanks (:


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

end

Now I would like to add this behaviour to other has_many relationships. But I would not like to copy paste the code... Any idea? :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-09-03 11:21:57

使用关联扩展:

class Profile < ActiveRecord::Base
  has_many :images do
    def page(limit=10, offset=0)
      all(:limit=> limit, :offset=>offset)
    end
  end
end

现在您可以使用 page 方法,如下所示:

@profile.images.page # will return the first 10 rows
@profile.images.page(20, 20) # will return the first 20 rows from offset 20
@profile.images # returns the images as usual

编辑

在这种特定情况下,关联功能可能是一个合适的选择。即使带有named_scope 的lambda 也可以工作。如果您在 Profile 类上定义它,您就会失去 named_scope 的可重用性。您应该在图像类上定义named_scope。

class Image < ActiveRecord::Base

  named_scope :paginate, lambda { |page, per_page| { :offset => ((page||1) -1) * 
                              (per_page || 10), :limit => :per_page||10 } }

end

现在您可以将这个named_scope与关联一起使用:

@profile.images.paginate(2, 20).all

或者您可以直接在Image类上使用named_scope

Image.paginate(2, 20).all(:conditions => ["created_at > ?" , 7.days.ago])

另一方面,为什么不使用will_paginate 插件?

Use association extensions:

class Profile < ActiveRecord::Base
  has_many :images do
    def page(limit=10, offset=0)
      all(:limit=> limit, :offset=>offset)
    end
  end
end

Now you can use the page method as follows:

@profile.images.page # will return the first 10 rows
@profile.images.page(20, 20) # will return the first 20 rows from offset 20
@profile.images # returns the images as usual

Edit

In this specific case, association function might be a suitable option. Even lambda with named_scope might work. If you define it on the Profile class you are loosing the reusable aspect of the named_scope. You should define the named_scope on your image class.

class Image < ActiveRecord::Base

  named_scope :paginate, lambda { |page, per_page| { :offset => ((page||1) -1) * 
                              (per_page || 10), :limit => :per_page||10 } }

end

Now you can use this named_scope with the association:

@profile.images.paginate(2, 20).all

Or you can use the named_scope directly on the Image class

Image.paginate(2, 20).all(:conditions => ["created_at > ?" , 7.days.ago])

On the other hand, why are you not using the will_paginate plugin?

逆流 2024-09-03 11:21:57

您可以使用 with_scope 将调用范围限定为 @profile.images,并在范围外执行计数。

Image.with_scope(:find => { :limit => 10, :offset => 10 }) do
  @profile.images      # load association using limit and offset
end

@profile.images.reset  # reset cached association, else size would return <=10
@profile.images.size   # go to the database again for a real COUNT(*)

You can use with_scope to scope your call to @profile.images, and perform the count outside the scope.

Image.with_scope(:find => { :limit => 10, :offset => 10 }) do
  @profile.images      # load association using limit and offset
end

@profile.images.reset  # reset cached association, else size would return <=10
@profile.images.size   # go to the database again for a real COUNT(*)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文