ruby on rails - What's the ideal way to model this with Mongoid? An issue with multiple models -
i noticing relation problem may have created.
i started off photo model. admin upload photos , exist on site.
in later iteration albums added codebase collection of photos organized on own page.
album has_many :photos, inverse_of: :album photo belongs_to :album, inverse_of: :photo
after playing above noticed photo can ever belong 1 album. has been ok.
now new issue arises. i'd add photos of members of staff own model.
i have special logic in photo model handle using specific upload service (filepicker).
staff field :name, type: string
how can make staff can have own photos? should create new model staffphoto denormalization? thinking of having photo either embedded or belong_to staff i'm not sure if makes sense belong both album , staff member. thing making sense me right creating separate model , abstracting upload service logic module , including both. wrong. important part photo's overall exist on own, or part of album, or part staff member (more 1 photo).
any suggestions on how model this?
a polymorphic association when have things belong different owner classes; e.g. when have photos either movie or tv-show, , have movies , tv-shows modeled in database -- you'd use polymorphic association refer "owner" object of photo.
typically not want model admins, staff, , users separate models. cleaner way model of them users, , have roles , permissions modeled separately, e.g. potentially admin.
if chose sort of modeling of users, need in photo model user_id model owner -- , don't care if that's admin or staff, or regular user.
if want model how photos used, e.g. admin photos different staff photos (e.g. admin photos backgrounds, staff photos head-shots), might want assign "photo type" attribute photos.
i don't think in scenario polymorphic association best solution.
please check out railscast on roles/authorization, can model admin/staff in 1 model.
http://railscasts.com/episodes?search=authorization
regarding embedded documents:
there few considerations using embedded documents:
- does embedded document augment/enrich parent document?
- not exceed 16mb mongodb record size
- if ever need search across embedded documents
in case of photos, want embed references photos, not photos themselves, because of large size photo can have.
in case of application, how many photos there user? can number grow considerable amount on time? if yes, don't want embed them.
does photo augment user record? e.g. avatar photo? if not, don't want embed it.
do ever need search across photos? not. other kinds of embedded document might need searched top level, , can not embed them.
Comments
Post a Comment