E
E
eldar_web2015-05-09 05:47:55
Ruby on Rails
eldar_web, 2015-05-09 05:47:55

Which link to use in Rails in this example?

There is a table Users (users) and Assistants (assistants), where each user can have several assistants (from users).
Moreover, Assistants have the following structure:
id | main_id (main) | user_id (assistant)
Which link is better to use here (and how), given that all data in Assistants will be taken from Users ???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
Jeiwan, 2015-05-09
@eldar_web

The Assistants table is not needed here, but you just need to set up the associations correctly:

User.rb
has_many :assistants, class_name: 'User', foreign_key: 'main_id', primary_key: 'id'
belongs_to :main, class_name: 'User'

Well, in Users you need to add the main_id column, in which the id of the main user will be indicated for the assistants. The master user will have nil in this column.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question