开发者

Mongoid Twitter-style following, can't specify criteria/conditions for relationship array

开发者 https://www.devze.com 2023-01-04 09:50 出处:网络
I\'m at my 开发者_JS百科wit\'s end trying to handle these errors.Basically, I\'ve created the following User and Relationship patterns, using Mongoid to handle my database.This seems like a near-carbo

I'm at my 开发者_JS百科wit's end trying to handle these errors. Basically, I've created the following User and Relationship patterns, using Mongoid to handle my database. This seems like a near-carbon copy of the example at the bottom of the page here. I'm trying to call any of the following:

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

These all seem to just return the entire relationships array; they don't search through by criteria. The find() method also throws an error saying that it only can take 1 argument. The im_following? method always returns true.

I'm not sure if it's better to post code in-line or from gist, so here are the gists:

user.rb

user_follow_spec.rb

relationship.rb

I would appreciate any help.


Rockmanioff, I have also came across the same issue. You might want to look at this as well. Mongoid plans on supporting this feature on their release candidate version. For now, we have to do things manually.

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 

In console, you can do:

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

In your case you might want to substitute "fan / fan_of" for "followers / following respectively". Hope this helps.


I'd advise you to simplify your relationships by using self-referential associations. Check out my answer to this question:

How-to: User has fans

I think this is pretty close to the association you want:

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


Try this:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消