I've a class Subscriber which has embeds_many Subscriptions. Subscription has an attribute status. I want to add a validation on status such that only one Subscription can have status 'active' per subscriber. The subscriber can have multiple subscription with status 'purchased' or 'expired' .
相关问题
- mongoid polymorphic association error
- Rails Mongoid `pluck` equivalent?
- How to make a query with or condition in mongoid
- Mongoid: how to query for all objects where value
- Need suggestions on designing artist recommendatio
相关文章
- Mongoid and ActiveRecord relations: undefined meth
- real polymorphism in embed relations in mongoid
- find_by_sql equivalent for mongoid?
- Connection refused - connect(2) with rake db:seed
- mongoid .limit does not work in mongoid 3.1.x
-
Rails 3: undefined method `page' for #
- Ember-data and MongoDB, how to handle _id
- Dynamically create index with mongoid
This should do it:
But make sure that you always call save on subscriber, otherwise the validations will not fire on subscriber and you will land in an inconsistent state. In an inconsistent state you might see failing validations later
If you need to also validate subscription, when saving subscriber, you cascade callbacks: