操作方法:用户有粉丝 [英] How-to: User has fans

查看:61
本文介绍了操作方法:用户有粉丝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用户能够成为其他用户的粉丝.我应该如何设计/设置?

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.

例如.我有用户:Foo. Foo有3个粉丝.我希望能够找到Foo粉丝的名字.因此:

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)

我得到: Mongoid :: Errors :: InvalidCollection:不允许访问Fan的集合,因为它是嵌入式文档,请从根文档访问集合.我认为是问题所在,因为风扇模型嵌入在用户模型中,该模型也具有对用户模型的自引用....

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....

您的想法将不胜感激.

推荐答案

如何进行自我参照关联:

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

问题是您试图仅使用嵌入式文档进行关系关联.如果在User中嵌入了Fan,则只能通过其父User访问Fan.您无法执行Fan.find(some_id)之类的操作,因为没有Fan记录的集合.

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.

最终,MongoDB将支持允许您执行此操作的虚拟集合.现在,您必须使用关系类型关联.如果要在这种情况下使用嵌入式文档,则必须创建一些丑陋且效率低下的自定义方法来搜索父记录.

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.

使用MongoDB和Mongoid,我发现您可以轻松地在嵌入式和关系关联之间进行切换. SQL类型的关系和嵌入的关系都有它们的位置,可以一起使用以产生很大的效果.

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.

这篇关于操作方法:用户有粉丝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆