问题描述
我正在尝试在我的视图中创建一个表并在某些条件下填充它.
I'm trying to create a table in my view and populate it on some conditions.
我有两张表,里面都有两列,一列叫做event_url
,另一列是gmiurl
.里面有gmiurl的表叫做GMITable
,另一个叫做newevent
I have two tables, Both have two columns inside, One columns called event_url
and the other is gmiurl
. The table with gmiurl inside is called GMITable
and the other is called newevent
基本上我想显示 GMITable
中的所有内容,除非 event_url
列中有一个与 gmiurls
中的任何 url 匹配的 urlGMITable
Basically i want to show everything inside the GMITable
unless the column event_url
has a url inside that matches to any of the urls inside gmiurls
inside the GMITable
除此之外,我目前没有任何代码
I dont have anycode for this apart from this at the moment
@GMI = GMITable.all
推荐答案
您可以通过使用从表 A 到表 B 的 LEFT OUTER JOIN 轻松实现结果,如 这个视觉解释.
You can easily achieve the result by using a LEFT OUTER JOIN from Table A to Table B, as described in this visual explanation.
具体来说,你想要的是:
Specifically, what you want is:
为了只在表 A 中生成记录集,而不在表 B 中生成记录集,我们执行相同的左外连接,然后通过 where 子句从右侧排除我们不需要的记录.
在您的问题中不清楚这些表格是如何命名的,以及它们如何相互关联.但是,要获得结果,只需使用 ActiveRecord join
方法
In your question is not clear how the tables are called, and how they relate each other. However, to achieve the result simply perform a join between the tables using the ActiveRecord join
method
TableA.joins('LEFT OUTER JOIN TableB on TableA.field = TableB.field')
并仅选择其中(TableB.id IS NULL
)的项目.
and select only the items where(TableB.id IS NULL
).
您必须根据自己的需要调整示例.
You'll have to adapt the example to your needs.
这篇关于Rails 哪里有两个表有相同的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!