0
有沒有辦法通過關聯來限制加載對象的數量?限制加載對象的數量
請看下面的例子:
class Person < ActiveRecord::Base
has_many :apples
end
class Apple < ActiveRecord::Base
belongs_to :person
end
我想所有的人,其裝載的蘋果......
Person.includes(:apples).limit(10)
...但我想限制apple
數對象每人加載到5,這個加載全部。
我曾嘗試以下:
在Person模型定義新關係的
class Person < ActiveRecord::Base has_many :apples has_many :limited_apples, class_name: 'Apple', limit: 5 end
使用
ActiveRecord::Associations::Preloader
people = Person.limit(50) ActiveRecord::Associations::Preloader.new(people, :limited_apples, limit: 5).run
這兩種方法都不能爲L模仿通過:apples
關聯加載的對象的數量。我是把自己放在兔子洞裏,還是錯過了明顯的東西?
是的,我幾乎得出了這個結論。雖然謝謝:) – 2013-03-14 18:40:34