我有一个友谊模式,包含一个状态。过滤协会由另一个协会
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的布尔变量。我希望能够通过隐藏设置为false的过滤我的用户的友谊。沿着这些线路的东西。
# In User Model
# Friendships
has_many :friendships do
def visible
# Where !friendship.status.hide
end
end
所以,在我的控制器我可以做这个
user.friendships.visible
我不知道如何访问个人友谊在这个方法虽然。
试试这个:'user.friendships.joins(:status).where(status:{hide:false})' – MrYoshiji