开发者

ROR: NOT EQUAL condition between two table

I have three tables: User, User_Interest, and Interest

How could I get the interest names that one of the users 开发者_开发知识库doesn't have?

(I have already set up the model for all of them.)

Thanks for your kindly help.


You could make this a class method on User, or an instance method on a particular user. For the class method, try this:

def self.missing_interests(user)
    difference = Interest.all - user.interests
    difference.map { |d| d.name }
end

and call it with User.missing_interests(some_user). For instance, try

def missing_interests
    difference = Interest.all - interests
    difference.map { |d| d.name }
end

and call with missing = some_user.missing_interests.

That should return an array with the names of all interests not included in the interests association for user.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜