ステータスを含む友情モデルがあります。
class Friendship < ActiveRecord::Base
attr_accessible :friend_id, :user_id, :source_id
after_create :check_friend_status
# Relationships
belongs_to :user, :touch => true
belongs_to :friend, :class_name => "User", :touch => true
belongs_to :source
has_one :status, :class_name => "FriendStatusDescriptor", :foreign_key => 'friendship_id'
validates_uniqueness_of :user_id, :scope => [:friend_id, :source_id]
def check_friend_status
# Check user/friend for existing friend status
if FriendStatusDescriptor.find(:first, :conditions => ["friendship_id = ?", self.id]).nil?
status = FriendStatusDescriptor.new
status.friendship_id = self.id
status.save
end
end
end
class FriendStatusDescriptor < ActiveRecord::Base
attr_accessible :alert, :friendship_id, :hide
belongs_to :friendship
validates_uniqueness_of :friendship_id
end
ステータス モデルには、hide というブール変数があります。hide が false に設定されているもので、ユーザーの友情をフィルタリングできるようにしたいと考えています。これらの線に沿った何か。
# In User Model
# Friendships
has_many :friendships do
def visible
# Where !friendship.status.hide
end
end
私のコントローラーでこれを行うことができるように
user.friendships.visible
ただし、この方法で個々の友情にアクセスする方法がわかりません。