Ruby on rails 3 Rails 3:ActiveRecord结果和数组#包括哪些内容?

Ruby on rails 3 Rails 3:ActiveRecord结果和数组#包括哪些内容?,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,以下是我的模型结构: A User has and belongs to many groups A Group has many subscriptions A Group has and belongs to many users A Group has many GroupAdmins A Subscription belongs to a group A GroupAdmin belongs to a user A GroupAdmin belongs to a Group 当我查询数

以下是我的模型结构:

A User has and belongs to many groups
A Group has many subscriptions
A Group has and belongs to many users
A Group has many GroupAdmins
A Subscription belongs to a group
A GroupAdmin belongs to a user
A GroupAdmin belongs to a Group
当我查询数据库(使用活动记录)时,我应该能够执行以下操作:

u = User.find 47238
在我的用户模型中,我有一个名为subscriptions的方法:

def subscriptions
    self.groups.where(:status => 'active').collect { |group| group.subscriptions.where("subscriptions.status = ? AND subscriptions.expiration_date > ?", 'active', "#{Time.zone.now}").last  }.flatten
  end
然后继续我的查询:

u.subscriptions.first.group.group_admins
上面返回了一个组的记录和我最初查询的用户id。。但由于某些原因,当我在组内运行此方法时,返回了一个false:

def is_group_admin?(user)
    self.group_admins.include? user
  end
工作代码:

def is_admin?(user)
    self.group_admins.collect { |ga| ga.user }.include? user
  end

group\u admins
返回GroupAdmin对象的集合。这些可能(通过其用户id)与用户相对应,但他们实际上不是用户,因此
组管理员.include?(user)
只能返回false


根据您正在做的事情,您可以在
group_admins.collect{ga | ga.user}
上调用
include?
,或者使用相应的
用户id
值搜索一个GroupAdmin。

谢谢您的建议!我将把工作代码放在上面