作者:捕鱼达人2502940243 | 来源:互联网 | 2023-01-27 19:54
So,Ihaveanappwithtwomodels.Foohas_oneBar,Barbelongs_toFoo.所以,我有一个有两个型号的应用程序。Foohas_o
So, I have an app with two models. Foo has_one Bar, Bar belongs_to Foo.
所以,我有一个有两个型号的应用程序。 Foo has_one Bar,Bar belongs_to Foo。
Now, to create a Foo you must to have to create a Bar to belong to it, but it looks like something slipped through the cracks because in my production app I now seem to have one Foo that somehow got created without a Bar, and it causes a 500 error.
现在,要创建一个Foo,你必须要创建一个属于它的Bar,但它看起来像是一些漏洞,因为在我的生产应用程序中,我现在似乎有一个Foo以某种方式创建没有Bar,它导致500错误。
Now, here's the problem:
现在,问题出在这里:
I can search: Bar.where(:foo=>nil)
just fine. But orphan bars aren't a problem, and this doesn't tell me what I need.
我可以搜索:Bar.where(:foo => nil)就好了。但孤儿棒不是问题,这并没有告诉我我需要什么。
I need to find the one Foo where Bar is nil. But the database stores the relationship in the Bars table, ie, BarsTable has foo_id in it, there's nothing in the FoosTable to tell me it's missing a bar.
我需要找到一个Foo,其中Bar是零。但是数据库将关系存储在Bars表中,即BarsTable中有foo_id,FoosTable中没有任何内容告诉我它缺少一个条形码。
When I use Foo.find(#).bar
I would get nil for the one bogus record, but I have a lot of records.
当我使用Foo.find(#)。栏时,我会为一个虚假的记录得到零,但我有很多记录。
So, can anyone tell me how to construct a query that would return the one Foo that is missing it's Bar?
那么,任何人都可以告诉我如何构建一个返回丢失它的Bar的一个Foo的查询吗?
Thanks!!
2 个解决方案