質問
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 => ???
設定方法を教えてくださいがします。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
解決
を使用する単一引用符の条件を囲むます:
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 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)
関係の中でヘビーデューティー条件を使用すると、原因のトラブルにバインドされています。可能であれば、それが簡単に照会するために、データ、または他のものの「簡単」のバージョンを持って派生列を作成、テーブルを非正規化してみてください。
まんて関係す。す:
- を拒否された友好の
profile_id
側 - を拒否された友好の
friend_id
側
なぜだかよくわかりませんの両面を拒否することができ、友だる上で重要な要素であるモデルのために少しこちら側が要求するのですか?できると考え、調査元に確認を取り消しの要求えで否決されたから profile
側?)
では、モデルにおいて、この二つの別々の関係におい
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"})
もう、このかわいらしい複雑ない。この状態は非常に複雑です。
所属していません StackOverflow