【问题标题】:Mongoid Twitter-style following, can't specify criteria/conditions for relationship arrayMongoid Twitter 风格的关注,不能为关系数组指定标准/条件
【发布时间】:2010-06-21 15:29:50
【问题描述】:

我在试图处理这些错误时已经束手无策了。基本上,我创建了以下用户和关系模式,使用 Mongoid 来处理我的数据库。这似乎是页面底部here 示例的近乎复本。我正在尝试拨打以下任何电话:

user1.relationships.find(:all, :conditions => {:rel_user => user_in_question, :rel_type => "following" })
user1.relationships.all(:conditions => {:rel_user => user_in_question, :rel_type => "following" })
user1.relationships.where(:rel_type => "following")
user1.relationships.following #with a named scope

这些似乎都只是返回整个关系数组;他们不按标准搜索。 find() 方法也会抛出一个错误,指出它只能接受 1 个参数。 im_following?方法总是返回 true。

我不确定是在线发布代码还是从 gist 发布代码更好,所以这里是要点:

user.rb
user_follow_spec.rb
relationship.rb

我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails database mongoid


    【解决方案1】:

    Rockmanioff,我也遇到了同样的问题。您可能还想查看this。 Mongoid 计划在他们的候选版本中支持这个特性。目前,我们必须手动操作。

    class User
      include Mongoid::Document
      include Mongoid::Timestamps
    
      references_many :fans, :stored_as => :array, :class_name => 'User', :inverse_of => :fan_of
      references_many :fan_of, :stored_as => :array, :class_name => 'User', :inverse_of => :fans
    
      def become_fan_of user
        fan_of << user
        self.save
    
        user.fans << self
        user.save
      end
    
      def is_a_fan? user
        fan_of_ids.include? user.id
      end
    
      def unfan user
        fan_of_ids.delete user.id
        self.save
    
        user.fan_ids.delete self.id
        user.save
      end
    
      ...
    end 
    

    在控制台中,你可以这样做:

    User.first.become_fan_of User.last
    User.first.is_a_fan? User.last
    User.first.unfan User.last
    

    在您的情况下,您可能希望将“fan / fan_of”替换为“followers / following”。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我建议您通过使用自引用关联来简化您的关系。看看我对这个问题的回答:

      How-to: User has fans

      我认为这与您想要的关联非常接近:

      class User
        include Mongoid::Document
        references_many :following, 
                        :class_name => 'User', 
                        :stored_as => :array, 
                        :inverse_of => :followed_by
      
        references_many :followed_by, 
                        :class_name => 'User', 
                        :stored_as => :array, 
                        :inverse_of => :following
      end
      
      # let's say we have users: al, ed, sports_star, movie_star    
      sports_star.followed_by << al
      movie_star.followed_by << al
      sports_star.followed_by << ed
      movie_star.followed_by << ed
      
      movie_star.followed_by  # => al, ed
      al.following            # => sports_star, movie_star
      

      【讨论】:

        【解决方案3】:

        试试这个:

        class User
        
          # follows and followers
          references_many :follows, :stored_as => :array , :inverse_of => :followers ,:class_name=>"User"
          references_many :followers, :stored_as => :array , :inverse_of => :follows ,:class_name=>"User"
        
        
          def followers
            followers.map 
          end
        
        end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-26
          • 2021-12-16
          • 1970-01-01
          • 2011-12-24
          相关资源
          最近更新 更多