I’m using Mongoid for my app and I have a problem setting up correct relationships for Users and subscriptions.
All I need to do is to make a simple “has one and belongs to one” relationship for UserSubscription model.
class User
has_many :user_subscriptions
end
class UserSubscription
belongs_to :user
has_one :user # user2 to which user1 is subscribed
field :category, :String
end
All I want to do is to have a list of subscriptions for each user:
> user1.user_subscriptions # list of subscription objects
> user1.user_subscriptions << UserSubscription.create(:user => user2, :category => 'Test')
> user1.user_subscriptions.where(:user => user2).delete_all
How to implement this? Thank you for help.
The problem is that you have two relations with the same name, and you need an inverse relation for your
has_one :userrelationship. You could always try something like this:Then you should be able to do things like: