There is such a table

enter image description here

Is it possible to refer to the lines by some unique name? Sort of

User.where(role: :admin) 
  • one
    It is possible, but these names must be stored somewhere. In your example, I do not see where the admin . - D-side
  • The question is where to keep these names? - Pavel
  • Yes, even in a separate column in the database, this is the first thing that comes to mind. - D-side

1 answer 1

If I understand correctly, then you want to make the distribution of users by roles. For this you have several options:

  1. Write the roles in a separate column of the same table (add the role column to the table)

  2. Make a separate roles table, and add a foreign key to the users table. That is, the column role_id appears in the users table. This will allow you to dynamically add / delete roles without touching the code.

  3. Make a separate roles table and a link table, for example users_roles and use links to through