I’m trying to include a module only when a condition is met.
module PremiumServer
def is_premium
true
end
end
class Server
include Mongoid::Document
include PremiumServer if self.premium
field :premium, :type => Boolean, :default => false
end
This isn’t working, and I can’t figure out why. Can someone please tell me how I’m supposed to include modules based upon a condition being met, like above?
Thanks!
EDIT:
I found the answer to my problem here: Mongoid and carrierwave
However, I’m awarding the question to the top answer as it is probably the more useful way.
includes happen on the class level. Your premium attribute is at instance level.
There are ways to do the include on per instance level, but I would not recommend them.
Here you are better of using inheritance
Or, in your case, if the only method is
is_premiumadd it to the Server class and have it return the premium variableoh, and you should use “question” method in ruby… Although Mongoid provides these for boolean values.