提交 981299e3 编写于 作者: R Ryuta Kamizono

Eager generate relation methods if a method is on `Kernel`

Follow up of #34122.

Relation method call is relying on method_missing, but if `Kernel` has
the same named method (e.g. `open`, etc), it will invoke Kernel's method
since method_missing is not happened.

To prevent that, eager generate relation methods if a method is the same
name on `Kernel`.

Fixes #39195.
上级 d9539281
......@@ -198,6 +198,10 @@ def scope(name, body, &block)
end
private
def singleton_method_added(name)
generate_relation_method(name) if Kernel.respond_to?(name)
end
def valid_scope_name?(name)
if respond_to?(name, true) && logger
logger.warn "Creating scope :#{name}. " \
......
......@@ -2119,6 +2119,8 @@ def test_relation_with_private_kernel_method
sub_accounts = SubAccount.all
assert_equal [accounts(:signals37)], sub_accounts.open
assert_equal [accounts(:signals37)], sub_accounts.available
assert_equal [topics(:second)], topics(:first).open_replies
end
def test_where_with_take_memoization
......
......@@ -9,6 +9,10 @@ class Reply < Topic
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
scope :ordered, -> { Reply.order(:id) }
def self.open
approved
end
end
class SillyReply < Topic
......
......@@ -42,6 +42,7 @@ def two
has_many :replies, dependent: :destroy, foreign_key: "parent_id", autosave: true
has_many :approved_replies, -> { approved }, class_name: "Reply", foreign_key: "parent_id", counter_cache: "replies_count"
has_many :open_replies, -> { open }, class_name: "Reply", foreign_key: "parent_id"
has_many :unique_replies, dependent: :destroy, foreign_key: "parent_id"
has_many :silly_unique_replies, dependent: :destroy, foreign_key: "parent_id"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册