In Rails 4, Active :: Relation automatisch delegierte Array Methoden wie slice
, so könnte man sagen, zum Beispiel SomeModel.where(prop: 'value').slice(0, 10)
Warum hat ActiveRecord :: Relation array_delegable entfernt? in Schienen 5?
, die in Schienen entfernt worden ist, 5. Warum?
Hier ist der relevante Code in Activerecord :: Relation in Schienen 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
Und hier ist es in 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
(Anmerkung: Ich habe versucht, diese Frage in posten das Rails-Forum, aber scheinbar ist die Registrierung dort kaputt und es gibt keine Möglichkeit, um Hilfe zu bitten)