---
title: "限制数量的 has_many 关联"
description: "限制数量的 has_many 关联"
canonical: https://oozou.com/til/limited-has_many-associations-31
locale: zh-Hans
date: 2020-10-13
topic: ruby-on-rails
---

# 限制数量的 has_many 关联

> 限制数量的 has_many 关联

Published: 2020-10-13 · Topic: [ruby on rails](https://oozou.com/zh-Hans/til-topics/ruby-on-rails) ([markdown](https://oozou.com/zh-Hans/til-topics/ruby-on-rails.md))

```ruby
# app/models/user.rb
has_one :four_car_garage
has_many :cars
validates :cars, length: { maximum: 4 }
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/zh-Hans/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
