Ruby on Rails Mongoid Embedded Associations

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Mongoid allows Embedded Associations:

  • One-to-one: embeds_one / embedded_in
  • One-to-many: embeds_many / embedded_in

To add an association (lets say the User embeds_many addresses), add this to your User file:

embeds_many :addresses

and this to your Address model file:

embedded_in :user

This will embed Address in your User model, adding a addresses method to your User class.



Got any Ruby on Rails Question?