如何在 .find 上使用 hash :condition 来表示 nil? Rails 中的方法(has_one 关系)

发布于 2024-09-25 07:51:29 字数 1517 浏览 3 评论 0原文

我想查找 :has_one 与 :contact_status 关系的值没有值(这是否意味着为零?)的所有联系人

我创建了一个 alias_attribute :status, :status_contact

我可以一起做些什么吗的台词:

contacts = Contact.find(:all, :conditions => {:contact_status => nil }, :include => {:status_contact} )

我不确定 nil 到底是如何工作的。基本上,我没有为它分配任何值,它只是一个 :has_one 关系。

编辑:听起来联系人应该是belongs_to关系,因此进行更改 从评论来看,听起来联系人实际上应该与 status_contacts 具有“belongs_to”关系,而“status_contacts”又与“联系人”具有“has_one”关系。

我正在使用 Searchlogic 创建named_scopes...我认为它解决了问题,有人熟悉吗?

然而,有时用户可以为其分配一个值。我不希望这些出现在查找中。

当前收到此错误:

SQLite3::SQLException: 靠近“.”: 语法错误: SELECT "contacts"."id" AS t0_r0, "contacts"."first_name" AS t0_r1,“联系人”。“姓氏” AS t0_r2,“联系人”。“标题” AS t0_r3,“联系人”。“电话” AS t0_r4,“联系人 "."传真" AS t0_r5,"联系人"。"电子邮件" AS t0_r6,"联系人"。"公司" AS t0_r7,"联系人"。"created_at" AS t0 _r8,“联系人”。“updated_at”AS t0_r9,“联系人”。“campaign_id”AS t0_r10,“联系人”。“输入日期”AS t0_ r11,“联系人”.“company_id” AS t0_r12,“联系人”.“address_id” AS t0_r13,“联系人”.“vcard_file_name” AS t0_r14,“联系人”。“vcard_content_type” AS t0_r15,“联系人”。“vcard_file_size” AS t0_r16,“联系人”。“vcar d_updated_at" AS t0_r17,"联系人"。"直接" AS t0_r18,"联系人"。"sugarcrm" AS t0_r19,"status_contacts"。 “id”AS t1_r0,“status_contacts”。“状态”AS t1_r1,“status_contacts”。“contact_id”AS t1_r2,“status_contac” ts"."created_at" AS t1_r3,"status_contacts"."updated_at" AS t1_r4 FROM "联系人" LEFT OUTER JOIN "状态 _contacts" ON status_contacts.contact_id = contacts.id WHERE (:status_contact.status = NULL)

I want to find all Contacts where the value of their :has_one relationship to :contact_status does not have a value (does that mean nil?)

I created an alias_attribute :status, :status_contact

Can I do something along the lines of:

contacts = Contact.find(:all, :conditions => {:contact_status => nil }, :include => {:status_contact} )

I'm not sure exactly how nil works. Basically, I don't assign any value to it, it is just a :has_one relationship.

EDIT: Sounds like contacts should be a belongs_to relationship so making change
From the comments, it sounds like Contacts should actually have a belongs_to relationship to status_contacts which in turn 'has_one' with Contacts.

I am using Searchlogic to create named_scopes...I think it solves the problem, anyone familiar with that?

However, there are times the user can assign a value to that. I don't want those to come up in the find.

Currently get this error:

SQLite3::SQLException: near ".": syntax error: SELECT "contacts"."id" AS t0_r0, "contacts"."first_name" AS
t0_r1, "contacts"."last_name" AS t0_r2, "contacts"."title" AS t0_r3, "contacts"."phone" AS t0_r4, "contacts
"."fax" AS t0_r5, "contacts"."email" AS t0_r6, "contacts"."company" AS t0_r7, "contacts"."created_at" AS t0
_r8, "contacts"."updated_at" AS t0_r9, "contacts"."campaign_id" AS t0_r10, "contacts"."date_entered" AS t0_
r11, "contacts"."company_id" AS t0_r12, "contacts"."address_id" AS t0_r13, "contacts"."vcard_file_name" AS
t0_r14, "contacts"."vcard_content_type" AS t0_r15, "contacts"."vcard_file_size" AS t0_r16, "contacts"."vcar
d_updated_at" AS t0_r17, "contacts"."direct" AS t0_r18, "contacts"."sugarcrm" AS t0_r19, "status_contacts".
"id" AS t1_r0, "status_contacts"."status" AS t1_r1, "status_contacts"."contact_id" AS t1_r2, "status_contac
ts"."created_at" AS t1_r3, "status_contacts"."updated_at" AS t1_r4 FROM "contacts" LEFT OUTER JOIN "status
_contacts" ON status_contacts.contact_id = contacts.id WHERE (:status_contact.status = NULL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自此以后,行同陌路 2024-10-02 07:51:29

您的错误与您显示的代码不符。
我猜你正在做类似的事情

:conditions => ['status = ?', nil]

,但你应该检查的不是 status,它是链接的对象。这是您在 ruby​​ 代码中检查它的方式,但不是在 sql 中检查它。

在表中,您将有一个名为 status_contact_id 的字段,当该字段为 NULL(数据库相当于 nil)时,它就没有关系。

因此,您确实需要执行类似

contacts = Contact.find(:all, :conditions => "status_contact_id IS NULL")

[编辑]的操作,因为您使用的是 has_one 关系,请完全阅读该关系,外键确实是在 status_contacts 表中定义的。但是,查询没有任何 status_contacts 的项目会变得有点复杂。

在 sql 中,你会做类似的事情

select * from contacts where id not in (select contact_id from status_contacts)

,我会将其翻译为 ruby​​,如下所示:

contacts = Contacts.find_by_sql("select * from contacts where id not in (select contact_id from status_contacts)"

就我个人而言,我现在想不出更好的方法。

Your error doesn't fit the code you are showing.
I guess you are doing something like

:conditions => ['status = ?', nil]

but what you should be checking is not the status, which is the linked object. It is how you would check it in ruby-code, but not in sql.

Inside your table, you would have a field called status_contact_id, and when that field is NULL (the database equivalent of nil) then it has no relation.

So you would indeed need to do something like

contacts = Contact.find(:all, :conditions => "status_contact_id IS NULL")

[EDIT] Since you are using a has_one relation, totally read over that, the foreign key is indeed defined in the status_contacts table. But, to query the items not having any status_contacts gets a bit more complicated.

In sql, you would do something like

select * from contacts where id not in (select contact_id from status_contacts)

and I would translate that to ruby like this:

contacts = Contacts.find_by_sql("select * from contacts where id not in (select contact_id from status_contacts)"

Personally i can't think of a better way right now.

和我恋爱吧 2024-10-02 07:51:29

尝试

contacts = Contact.find(:all, :conditions => "status_contact_id IS NULL")

假设您的 fk 列是 contacts.status_contact_id

更新: nathanvda 是正确的;另外,我认为您可以在没有 find_by_sql 的情况下完成此操作 - 未经测试...

contacts = Contact.find(:all, 
    :conditions => "contacts.id NOT IN (select contact_id from status_contacts)"   
)

注意: 我仍然认为您确实想使用 Contact 与 own_to :status_contact

try

contacts = Contact.find(:all, :conditions => "status_contact_id IS NULL")

assuming your fk column is contacts.status_contact_id

UPDATE: nathanvda is correct; Also, I think you can do it without find_by_sql - untested...

contacts = Contact.find(:all, 
    :conditions => "contacts.id NOT IN (select contact_id from status_contacts)"   
)

NOTE: I still think you really want to use Contact with belongs_to :status_contact

我早已燃尽 2024-10-02 07:51:29

取决于 Rails 的版本,我主要在 2.3.5 中工作,但我相信在这种情况下“条件”意味着“类似 SQL 的片段”。

通常你会做类似的事情

contacts = Contact.find(:all, :include => :contact_status, :conditions => ["contact_status.status = ?", nil])

,此外,nil 在 Ruby 中意味着完全没有价值。它什么也不存储。 [] 是一个空数组,如果您向其发送消息 nil?,它将返回 false,但是,它会对消息 empty? 响应 true。

Depends on the version of Rails, I mostly work in 2.3.5, but I believe "conditions", in this context, is meant to be a "SQL-like fragment."

Typically you'd do something like

contacts = Contact.find(:all, :include => :contact_status, :conditions => ["contact_status.status = ?", nil])

Also, nil in Ruby means completely devoid of value. It stores nothing. [], an empty array, will return false if you send it the message nil?, it will respond true to the message empty? however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文