2011-06-06 51 views

回答

4

所以,如果我理解你的问题,你有什么是一样的东西:

class User < ActiveRecord::Base 
    has_many :groups, :though => :user_members 

class Group < ActiveRecord::Base 
    has_many :users, :though => :user_members 

而且你要知道,如果用户是特定组的成员。

因此,鉴于这样的:

u = User.first 
g = Group.first 

只要做到:

u.groups.include? g 

那一切就是这么简单!

伊恩。

+0

+1一个很漂亮的红宝石办法做到这一点。 – Spyros 2011-06-06 06:25:46

+0

是的,太棒了! – user681177 2011-06-06 13:43:47

0

IPD的方式是好的,面向办法做到这一点另一个更Rails是:

u.groups.exists?(g) 
相关问题