# Association Scope

场景"关联的时候，关联表带有 scope"，不使用它的话，生成的 SQL 里能看到有的条件是重复的。Association Scope 解决了这个问题。

另，看示例还有另外的好处：

```ruby
class Review < ActiveRecord::Base
  belongs_to :restaurant

  scope :positive, -> { where("rating > 3.0") }
end

class Restaurant < ActiveRecord::Base
  has_many :reviews
  has_many :postitive_reviews, -> { positive }, class_name: "Review"
end
```

```ruby
restaurants = Restaurant.includes(:positive_reviews).first(5)
```

Collection Association、Singular Associations 和 Association 调用了它。


---

# 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/activerecord_association_achitecture/association_others/association_associationscope.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.
