我需要提取所有具有内容设置的内容和不具有内容设置的内容。

huangapple go评论78阅读模式
英文:

I need to fetch all the content which has content settings and which doesn't

问题

我需要获取所有具有内容设置和没有内容设置的内容,同时使用includes方法。内容设置表中有与content_id和编辑类型相关联的关联。

我之前使用左外连接做过这个操作,但我想使用includes方法执行这个查询。

英文:

I need to fetch all the content which has content settings and which doesn't, using both the includes methods. There is an association in the contentSetting table with the content_id and editor type.

I had done it with left outer join earlier but I have to do it with includes.

contents = Content.all
contents = contents
  .left_outer_joins(:content_setting)
  .where('content_settings.editor_type = ? OR content_settings.id IS NULL', '0')

but I want to perform this query using includes method.

答案1

得分: 1

我认为references在这种情况下应该有所帮助:

Content
  .includes(:content_setting)
  .references(:content_settings)
  .where('content_settings.editor_type = ? OR content_settings.id IS NULL', '0')
英文:

I think references should help in this case:

Content
  .includes(:content_setting)
  .references(:content_settings)
  .where('content_settings.editor_type = ? OR content_settings.id IS NULL', '0')

答案2

得分: 0

baseq = Content.includes(:content_setting)
baseq.where.missing(:bars).or(baseq.where(content_settings: { editor_type: 0 }))
英文:
baseq = Content.includes(:content_setting)
baseq.where.missing(:bars).or(baseq.where(content_settings: { editor_type: 0 }))

huangapple
  • 本文由 发表于 2023年6月29日 22:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定