Ruby on rails Rails查询关联(连接)和#x27;富文本';不是零/空
我现在有以下内容,可以成功地生成一个用户列表,其中包含他们的Ruby on rails Rails查询关联(连接)和#x27;富文本';不是零/空,ruby-on-rails,ruby,Ruby On Rails,Ruby,我现在有以下内容,可以成功地生成一个用户列表,其中包含他们的rich\u text\u tlk\u和\u me“rich\u text”附件 User.joins(:rich_text_tlk_with_me).paginate(page: params[:page], per_page: 5).order(created_at: :desc) 但是,目前此查询还显示了具有空富文本附件的用户 User.joins(:rich_text_tlk_with_me).paginate(page: p
rich\u text\u tlk\u和\u me
“rich\u text”附件
User.joins(:rich_text_tlk_with_me).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
但是,目前此查询还显示了具有空富文本附件的用户
User.joins(:rich_text_tlk_with_me).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
我想运行一个查询,该查询只返回具有非空富文本附件的用户
我试过:
User.joins(:rich_text_tlk_with_me).where.not(rich_text_tlk_with_me: [nil, ""]).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
但这并没有带来任何回报
我也搜索了文档,但没有找到任何相关的内容 假设
用户有很多:富文本和mes,并且您使用的是Rails版本>5
通过以下查询,您可以获取所有具有关联的富文本\u tlk\u和\u me
的用户
User.left_outer_joins(:rich_text_tlk_with_mes).where.not(rich_text_tlk_with_mes: {id: nil}).paginate(page: params[:page], per_page: 5).order(created_at: :desc)
感谢您的回答,我实际上意识到我的查询是成功的(oops!),但没有处理用户删除内容的情况。相反,我将添加一个“销毁附件”路由,这样不想被列出的用户就可以删除自己。谢谢你的帮助