# Concern

可以方便快捷的扩展某个类或模块，并且处理了潜在的依赖问题。

作用有二：

1\) 更简洁、明了的语法。 \
&#x20;2\) 更好的处理模块之间的依赖关系，规避潜在的模块之间的循环依赖。

约定：ClassMethods 和 class\_methods 自动继承、实例方法自动包含、自动使用 class\_eval.

原来你需要手动写 included 或 extended 里面的代码，有实例方法、类方法的话，也要手动包含或继承；现在按照约定来即可。

以前：

```ruby
module M
  def self.included(base)
    base.extend ClassMethods

    base.class_eval do
      # 执行某些方法
      scope :disabled, -> { where(disabled: true) }

      # 执行某些方法
      include InstanceMethods
    end
  end

  # 定义类方法
  module ClassMethods
    # ...
  end

  # 定义实例方法
  def a_instance_method
    # ...
  end

  # 如果实例方法比较多，可以单独成 module，对应上面的 include InstanceMethods
  module InstanceMethods
    # ...
  end
end
```

现在：

```ruby
require 'active_support/concern'

module M
  extend ActiveSupport::Concern

  included do
    # 执行某些方法
    scope :disabled, -> { where(disabled: true) }

    # 执行某些方法
    include InstanceMethods
  end

  # 定义类方法
  module ClassMethods
    # ...
  end

  # 定义实例方法
  def a_instance_method
    # ...
  end

  # 如果实例方法比较多，可以单独成 module，对应上面的 include InstanceMethods
  module InstanceMethods
    # ...
  end
end
```

本模块源代码、及示例已经经过多次更改，多学语法，适应改变适应其变化。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kelby.gitbook.io/rails-beginner-s-guide/activesupport_other_class_and_module/activesupport_concern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
