1

私はこれらのエラーを処理しようとして頭がいっぱいです。基本的に、Mongoid を使用してデータベースを処理し、次のユーザー パターンと関係パターンを作成しました。これは、こちらのページの下部にある例のほぼカーボン コピーのようです。私は次のいずれかを呼び出そうとしています:

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

助けていただければ幸いです。

4

3 に答える 3

1

これを試して:

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
于 2010-11-22T10:26:23.547 に答える
1

自己参照関連を使用して関係を簡素化することをお勧めします。この質問に対する私の答えをチェックしてください:

ハウツー: ユーザーにファンがいる

これはあなたが望む関連付けにかなり近いと思います:

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
于 2010-11-22T09:52:22.050 に答える
1

ロックマニオフ、私も同じ問題に遭遇しました。これも見てみるといいかもしれません。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

あなたの場合、「followers/followers」を「fan/fan_of」に置き換えたいと思うかもしれません。お役に立てれば。

于 2010-11-29T10:51:33.973 に答える