开发者

How do you set up this association?

开发者 https://www.devze.com 2023-03-09 08:44 出处:网络
I want a \"group\" to hav开发者_开发知识库e many members. I already have a user and a profile model, and if a user identifies itself as a \"group\", I want it to be able to list its members. Therefore

I want a "group" to hav开发者_开发知识库e many members. I already have a user and a profile model, and if a user identifies itself as a "group", I want it to be able to list its members. Therefore, I'm thinking of letting a profile has_many :members, :class_name => 'User' so that a "group" can select existing users. However, I also want to let a group list members that are not existing users. How would I set up this association? Am I doing this correctly?


"let a group list members that are not existing users" -- do you mean "let a group list users that are not existing members"?

If so, then this answer I gave a while back might help: How to find all items not related to another model - Rails 3


Try with this lambda in your User model:

named_scope :not_members, lambda { |profile_id| where("id NOT IN (?)", 
                                   User.where( :profile_id => profile_id ).map(&:id) ) }

later in your code:

@not_members = User.not_members(some_profile_id)

PS: This is not tested

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号