Руководство по Ruby on Rails: Active Record: базовые ассоциации | Participants
|
- Statistics
- Participants
- Translate into Russian
- Translation result
- 19% translated in draft.
If you do not want to register an account, you can sign in with OpenID.
Ruby on Rails Guides: Active Record: Association basics | Руководство по Ruby on Rails: Active Record: базовые ассоциации | |
A Guide to Active Record Associations | ||
This guide covers the association features of Active Record. By referring to this guide, you will be able to: | В этом руководстве рассматриваются вопросы отношений в Active Record. Тут вы научитесь: | |
* Declare associations between Active Record models | * Описывать отношения между моделями Active Record. | |
== Why Associations? | ||
Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this: | Зачем нам нужны отношения между моделями? Они делают обычные операции проще. Например, возьмём обычное Rails-приложение, включающее модели Customer (покупатели) и Order (заказы). У каждого покупателя может быть много заказов. Без отношений модели буду выглядеть следующим образом: | |
[source, ruby] | [source, ruby] | |
class Order < ActiveRecord::Base | ||
Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this: | Теперь, предположим мы хотим добавить новый заказ для существующего пользователя. Мы должны делать так: | |
[source, ruby] | [source, ruby] | |
Or consider deleting a customer, and ensuring that all of its orders get deleted as well: | Или мы решаем удалить пользователя, равно как и все его заказы: | |
[source, ruby] | [source, ruby] | |
With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders: | С отношениями в Active Record мы можем сократить эти и другие операции, объявив Rails, что две модели связаны между собой. Вот код для соответствующей настройки пользователей и заказов. | |
[source, ruby] | [source, ruby] | |
class Order < ActiveRecord::Base | class Order < ActiveRecord::Base |

— этот текст уже переводится под другим именем: http://translated.by/you/ruby-on-rail... — and_rew