S
S
Sahnen2016-11-16 11:54:29
Ruby on Rails
Sahnen, 2016-11-16 11:54:29

How to choose the necessary associations?

For example, there is a system of statistics. It consists of the following models:
- Region ( Region )
- Field of activity ( Job ). For example: production, trade, services, etc.
- Clarification of the type of activity ( JobItem ). For example, if we are talking about services: advertising, transportation, repairs, etc.
- Specific statistical unit ( Company ). For example, a particular enterprise.
The following dependencies arise:
Job - JobItem everything is clear - has_many/belongs_to
And then not everything is clear.
Set Job / JobItemcan be present in any region and does not change from region to region. Those. has_many/belongs_to between Region and Job makes no sense to set.
Company , in turn, directly depends, on the one hand, on the Region (it can belong to only one region), on the other hand, it also directly belongs to only one JobItem (for example, the main activity).
Naturally, I would like to apply various filters later. For example:
- Businesses providing advertising services in all regions
- Businesses providing advertising services in a specific region
- All businesses in a specific region
- All enterprises in a certain region that are engaged in trade
, etc.
Of course, you can do without associations at all, but I would like flexibility so as not to clutter up the code for each condition with a bunch of query chains. Perhaps you can set up associations in your mind?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anna Buyanova, 2016-11-16
@Sahnen

I don't understand what the problem is:
Company/JobItem - belongs_to/has_many
Company/Region - belongs_to/has_many
Region and JobItem are not connected in any way.
In real life, it often happens that companies are engaged in several activities, but this is another problem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question