1
    c = "(f.profile_id = #{self.id} OR f.friend_id = #{self.id})"
    c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.friend_id ELSE f.profile_id END = p.id)"
    c += AND + "(CASE WHEN f.profile_id=#{self.id} THEN f.profile_rejected ELSE f.friend_rejected END = 1)"
    c += AND + "(p.banned = 0)"

これを次のような has_many 関係で使用する必要があります。

    has_many :removed_friends, :conditions => ???

self.id をそこに設定するにはどうすればよいですか? または、そこに ID を渡すにはどうすればよいですか? 次に、will_paginate プラグインを使用します。

    @profile.removed_friends.paginate(:page => 1, :per_page => 20)

ご協力いただきありがとうございます

編集:

 class Profile < ActiveRecord::Base
    has_many :friendships
    has_many :removed_friends, :class_name => 'Profile', :through => :friendships, :conditions => 
        "(friendships.profile_id = #{self.id} OR friendships.friend_id = #{self.id})"
        "AND (CASE WHEN friendships.profile_id=#{self.id} THEN friendships.profile_rejected ELSE friendships.friend_rejected END = 1)" + 
        "AND (p.banned = 0)"
  end


class Friendship < ActiveRecord::Base
  belongs_to :profile
  belongs_to :removed_friend, :class_name => 'Profile', :foreign_key => "(CASE WHEN friendships.profile_id = #{self.id} THEN friend_id ELSE profile_id END)"
end
4

3 に答える 3

3

一重引用符を使用して条件を囲みます。

class Profile < ActiveRecord::Base
  has_many :friendships
  has_many :removed_friends, :class_name => 'Profile', :through => :friendships, 
                             :conditions => '
    ( friendships.profile_id = #{self.id} OR 
      friendships.friend_id = #{self.id}
    ) AND
    (CASE WHEN friendships.profile_id=#{self.id} 
          THEN friendships.profile_rejected 
          ELSE friendships.friend_rejected 
     END = 1
    ) AND 
    (p.banned = 0)'
end
于 2010-03-30T17:49:40.487 に答える
1

これを、一度にすべてではなく段階的に適用できる一連の名前付きスコープに分割することをお勧めします。例として、禁止された部分を抽出します。

class Friend < ActiveRecord::Base
  named_scope :banned, lambda { |*banned| {
    :conditions => { :banned => banned.empty? ? 1 : (banned.first ? 1 : 0) }
  }}
end

@profile.friends.removed.banned(false).paginate(:page => 1, :per_page => 20)

人間関係で過酷な条件を使用すると、問題が発生する可能性があります。可能であれば、テーブルの非正規化、データの「簡単な」バージョンを含む派生列の作成、またはクエリを簡単にするその他の方法を試してください。

于 2010-03-30T17:51:35.877 に答える
0

ここには 2 つの関係があります。あなたが持っている:

  • profile_id側から拒絶された友情
  • friend_id側から拒絶された友情

双方が友情を拒否できる理由がわかりません。おそらく、ここでモデルを少し確認する必要があります (どちらの側がそれを要求していますか? 要求者がそれを言うのではなく、要求をキャンセルしたと考えた方がよいでしょうか?側から拒否されたprofile?)

いずれにせよ、私はこれを次の 2 つの別個の関係としてモデル化します。

class Profile
  has_many :rejected_friendships, :conditions => 'friendships.profile_rejected = 1'
  has_many :canceled_friendships, :foreign_key => 'friend_id', :conditions => 'friendships.friend_rejected = 1'

  named_scope :banned, lambda do |*banned| 
      { :conditions => {:banned => banned.empty? ? 1 : (banned.first ? 1 : 0) } }
  end

  has_many :rejected_friends, :class_name => 'Profile', :through => :rejected_friendships
  has_many :canceled_friends, :class_name => 'Profile', :through => :canceled_friendships

  def removed_friends
    (self.rejected_friends.banned(false).all + self.canceled_friends.banned(false).all).uniq
  end
end

これは、removed_friends がもう関係ではないため、やや望ましくないため、このようなことはできなくなりますがProfile.removed_friends.find(:all, :conditions => {:name => "bleh"})、これはかなり複雑なケースです。その条件は非常に複雑です。

于 2010-03-30T20:04:12.880 に答える