1
在Rails 4,ActiveRecord的::關係自動委託陣列的方法,如slice
,所以你可以說,例如SomeModel.where(prop: 'value').slice(0, 10)
爲什麼ActiveRecord :: Relation刪除array_delegable?在Rails 5中?
已在滑軌5.爲什麼被刪除?
這裏是ActiveRecord的相關代碼:4的關係在導軌:
def array_delegable?(method)
Array.method_defined?(method) && BLACKLISTED_ARRAY_METHODS.exclude?(method)
end
def method_missing(method, *args, &block)
if @klass.respond_to?(method)
scoping { @klass.public_send(method, *args, &block) }
elsif array_delegable?(method)
to_a.public_send(method, *args, &block)
elsif arel.respond_to?(method)
arel.public_send(method, *args, &block)
else
super
end
end
這裏,它是Rails中5:
def method_missing(method, *args, &block)
if @klass.respond_to?(method)
scoping { @klass.public_send(method, *args, &block) }
elsif arel.respond_to?(method)
arel.public_send(method, *args, &block)
else
super
end
end
(注:我試過張貼在這個問題上Rails論壇,但顯然註冊是打破那裏,並且沒有辦法尋求幫助)