Rails - how to code 'may belong?

I am working on a Rails application that will contain information about a group of people. There are two use cases:

  • The administrator enters and maintains the user profile
  • A person can decide to register, log in and save their profile

I'm trying to figure out the relationship between a profile and a user. Right now, that profile belongs_to :userand user has_one :profile.

But this breaks down because some profiles do not yet have a user - they are supported by the administrator. In this case, the column user_idin profilesis null, the connection fails, and the page does not load. ++

How can I set up a “possibly owned” relationship?

++ (In fact, it looks empty, displays without error, and development.logalso does not have any errors, but I see that it does not finish all the requests that it performs for a profile that has If anyone knows how I can get useful error message, this will also be great.)

+3
source share
2 answers

This is the right way to do this. belongs_todoesn't interfere with the values null, and it looks like what you want.

The "page" for a profile that does not yet belong to the user may not have so many requests, because there is no need (or path) to query the user table for this profile.

profile.user nil .

+4

a Profile a User, ( , Engine Car). Profile.belongs_to :user.

Profiles User, . :

,

, , ? , .

0

Source: https://habr.com/ru/post/1738022/


All Articles