ActiveRecord associations: any gotchas if has_many WITHOUT matches belongs_to?

The phone has a lot of messages.

The email address is full of messages.

The message either belongs to the phone, or email, or not. Communication belongs_tois optional.

The following associations seem to work fine for this relationship:

  • Phone model has_many :messages
  • Email Model has_many :messages
  • The message model has no belongs_to :phones, :email

Is this good or is there some kind of correct way to specify the relationship "can_belong_to"?

+5
source share
2 answers

. "curcular dependency" validates_associated.

, has_many :messages, , . , .

+4

belongs_to (, messages phone_id email_id).

belongs_to, has_many, :

phone.messages
message.phone

, belongs_to FK has_many .

, " ", has_and_belongs_to_many, .

+1

All Articles