How-to: User has fans
I need users to be able to become fans of other users. How should I design/set this up?
I need to be able to view details of user fans.
For example. I have user: Foo. Foo has 3 fans. I'd like to be able to find the names of Foo's fans. As such:
foo = User.first
foo.name (returns 'Foo')
foo.fans.first.user.name (should return 'bar', since 'bar' is a fan of 'foo')
This is how I have it set up at the moment:
User model:
embeds_many :fans
references_many :fans
Fan model:
embedded_in :user, :inverse_of => :fans
referenced_in :user
In console, I do:
User.first.fans.create!(:user => User.first)
and I get: Mongoid::Errors::InvalidCollection: Access to the collection for Fan is not allowed since it is an embedded document, please access a collection from the root document. I think the problem is, because the fan model is embedded in the user model which has a self-reference to the user model as well....开发者_运维百科
Your thoughts will be much appreciated.
How about a self-referential association:
class User
include Mongoid::Document
references_many :fans,
:class_name => 'User',
:stored_as => :array,
:inverse_of => :fan_of
references_many :fan_of,
:class_name => 'User',
:stored_as => :array,
:inverse_of => :fans
end
# let's say we have users: al, ed, sports_star, movie_star
sports_star.fans << al
movie_star.fans << al
sports_star.fans << ed
movie_star.fans << ed
movie_star.fans # => al, ed
al.fan_of # => sports_star, movie_star
The problem is that you are trying to do relational association using only embedded documents. When you have a Fan
embedded inside a User
, you can only access the Fan
through its parent User
. You can't do something like Fan.find(some_id)
because there is no collection of Fan
records.
Eventually, MongoDB will support virtual collections that will allow you to do this. For now, you have to use relational-type associations. If you want to use embedded documents in this case, you have to create some ugly and inefficient custom methods to search through parent records.
With MongoDB and Mongoid, I have found that you can switch between embedded and relational associations easily. SQL-type relationships and embedded relationships both have their place and can be used together to great effect.
精彩评论