【问题标题】:Rails associations SQLRails 关联 SQL
【发布时间】:2019-09-30 12:47:47
【问题描述】:

我有一个 User 模型和一个 SkillList 模型。

协会 - has_many / belongs_to.

所以我可以找到一个用户的技能列表条目:

current_user.skill_list

用户表有字段:idnamelocation

我需要查询数据库找到所有有位置和技能的用户。

我可以找到所有具有现有位置的用户(不包括当前用户):

users = User.where.not(location: nil, id: current_user.id)

但是如何排除没有技能的用户呢?

【问题讨论】:

    标签: sql ruby-on-rails ruby associations


    【解决方案1】:

    您应该简单地使用joins,它默认执行SQL INNER JOIN,这正是您在这种情况下所需要的:

    User.where.not(location: nil).joins(:skill_list)
    

    【讨论】:

    • 是的,这就是我需要的!我不明白在哪里设置joins。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多