ruby - Modeling a Subscription in Rails -
so, after thinking on while, have no thought proper way model is.
i have website focused on sharing images. in order create users happy, want them able subscribe many different collections of images.
so far, there's 2 types of collections. 1 "creator" relationship, defines people worked on specific image. looks this:
class image < activerecord::base has_many :creations has_and_belongs_to_many :locations has_many :creators, through: :creations end class creator < activerecord::base has_many :images, ->{uniq}, through: :creations has_many :creations belongs_to :user end class creation < activerecord::base belongs_to :image belongs_to :creator end
users may tag image subjective tag, not objectively nowadays in image. typical subjective tags include "funny" or "sad," kind of stuff. that's implemented this:
class subjectivetag < activerecord::base # has "name" field. reason why names of tags first-class db model # can display how many times given image has been tagged specific tag end class subjectivecollection < activerecord::base # basically, "user x tagged image y tag z" belongs_to :subjective_tag belongs_to :user has_many :images, through: :subjective_collection_member end class subjectivecollectionmember < activerecord::base belongs_to :subjective_collection belongs_to :image end
i want users able subscribe both creators , subjectivetags, , display images in collections, sequentially, on home page when log in.
what best way this? should have bunch of different subscription types - example, 1 called subjectivetagsubscription
, 1 called creatorsubscription
? if go route, efficient way retrieve images in each collection?
what want utilize polymorphic association.
in case, this:
class subscription < activerecord::base belongs_to :user belongs_to :subscribeable, polymorphic: true end
the subscriptions
table need include next fields:
user_id
(integer) subscribeable_id
(integer) subscribeable_type
(string) this setup allow subscription refer instance of other model, activerecord utilize subscribeable_type
field record class name of thing beingness subscribed to.
to produce list of images logged in user, this:
subscription.where(user_id: current_user.id).map |subscription| subscription.subscribeable.images.all end.flatten
if performance implications of above approach intolerable (one query per subscription), collapse 2 types of subscribeables single table via sti (which doesn't seem thought here, 2 tables aren't similar) or go initial suggestion of having 2 different types of subscription models/tables, querying each 1 separately subscriptions.
ruby-on-rails ruby database rails-activerecord
No comments:
Post a Comment