Reputation: 387
I have been using rails and mongoid and have the following models (as an example):
class SocialMedia
end
class Facebook < SocialMedia
include Mongoid::Document
field :fans
end
class Instagram < SocialMedia
include Mongoid::Document
field :fans
end
I wanted to move both "fans" fields from Facebook and Instagram to SocialMedia class (inherited) WITHOUT changing collections. If I do something like:
class SocialMedia
include Mongoid::Document
field :fans
end
I end up with a new collection called social_media with _type: being either facebook or instagram. Since this database is already quite huge, it is not an option to update that.
Any ideas?
Upvotes: 2
Views: 1425
Reputation: 102250
The collection for the model's documents can be changed at the class level if you would like them persisted elsewhere. - http://mongoid.org/en/mongoid/v3/documents.html
class SocialMedia
include Mongoid::Document
field :fans
end
class Facebook < SocialMedia
store_in collection: "facebook"
end
class Instagram < SocialMedia
store_in collection: "instagram"
end
Or
class SocialMedia
include Mongoid::Document
field :fans
store_in collection: model_name.plural
end
Upvotes: 0
Reputation: 3837
You can do that using concerns. Create a concern like this
module Sociable
extend ActiveSupport::Concern
included do
field :fans
end
end
And then include this module in both your models.
Upvotes: 2