Writing sample data:
- started with small amount from Study Buddy - need to follow "Contact Book - Our Very First Database" from AppDev1 section "custom rake tasks"
Association Accessors:
Direct Associations: belongs_to
In standard Rails applications, the default is opposite: belongs_to adds an automatic validation to foreign key columns enforcing the presence of a valid value unless you explicitly add the option optional: true.
So: if you decided to remove the null: false database constraint from any of your foreign key columns in the migration file (e.g., change t.references :recipient, null: false ... to t.references :recipient, null: true ...), then you should also add the optional: true option to the corresponding belongs_to association accessor.
So remember — if you’re ever in the situation of:
- you’re trying to save a record
- the save is failing
- you’re doing the standard debugging technique of printing out
zebra.errors.full_messages - you’re seeing an inexplicable validation error message saying that a foreign key column is blank
- now you know where the validation is coming from:
belongs_toadds it automatically - so figure out why you’re not providing a valid foreign key (usually it is because the parent object failed to save for its own validation reasons)
Direct associations: has_many
Each belongs_to, there should be an inverse has_many. Check to make sure you have all of these inverse 1-N associations.
attendee ~ fan
leaders = who a user is following
Top comments (0)