思考 Sphinx 和多个 has_one
我的模型中有多个 has_one 关联,
class Invoice < ActiveRecord::Base
...
belongs_to :seller, :class_name => "Client", :foreign_key => "provider", :conditions => ['is_provider = ?', true]
belongs_to :customer, :class_name => "Client", :foreign_key => "receiver", :conditions => ['is_receiver = ?', true]
我正在尝试对其进行索引:
indexes [seller(:tbClientCode), seller(:tbClientLabel), seller(:tbClientName), seller(:tbClientNIP),
seller(:tbClientRegon), seller(:tbClientZip), seller(:tbClientCity), seller(:tbClientStreet),
seller(:tbClientHouseNr), seller(:tbClientHomeNr], :sortable => true, :as => :seller_fields
has [seller(:is_provider), seller(:is_receiver)], :sortable => true, :as => :seller_attributes
indexes [customer(:tbClientCode), customer(:tbClientLabel), customer(:tbClientName), customer(:tbClientNIP),
customer(:tbClientRegon), customer(:tbClientZip), customer(:tbClientCity), customer(:tbClientStreet),
customer(:tbClientHouseNr), customer(:tbClientHomeNr], :sortable => true), :as => :customer_fields
has [customer(:is_provider), customer(:is_receiver)], :sortable => true, :as => :customer_attributes
...然后发生一些错误
ERROR: index 'invoice_core': sql_range_query: ERROR: column reference "is_receiver" is ambiguous
LINE 1: ...tomers_invoices"."id" = "invoices"."receiver" AND is_receive...
将语法从 customer(:tbClientName) 更改为 customer.tbClientName 不是解决方案。
一些帮助将不胜感激
I've got multiple has_one associations in my model
class Invoice < ActiveRecord::Base
...
belongs_to :seller, :class_name => "Client", :foreign_key => "provider", :conditions => ['is_provider = ?', true]
belongs_to :customer, :class_name => "Client", :foreign_key => "receiver", :conditions => ['is_receiver = ?', true]
I'm trying to index it:
indexes [seller(:tbClientCode), seller(:tbClientLabel), seller(:tbClientName), seller(:tbClientNIP),
seller(:tbClientRegon), seller(:tbClientZip), seller(:tbClientCity), seller(:tbClientStreet),
seller(:tbClientHouseNr), seller(:tbClientHomeNr], :sortable => true, :as => :seller_fields
has [seller(:is_provider), seller(:is_receiver)], :sortable => true, :as => :seller_attributes
indexes [customer(:tbClientCode), customer(:tbClientLabel), customer(:tbClientName), customer(:tbClientNIP),
customer(:tbClientRegon), customer(:tbClientZip), customer(:tbClientCity), customer(:tbClientStreet),
customer(:tbClientHouseNr), customer(:tbClientHomeNr], :sortable => true), :as => :customer_fields
has [customer(:is_provider), customer(:is_receiver)], :sortable => true, :as => :customer_attributes
... and then some error occurs
ERROR: index 'invoice_core': sql_range_query: ERROR: column reference "is_receiver" is ambiguous
LINE 1: ...tomers_invoices"."id" = "invoices"."receiver" AND is_receive...
Changing syntax from customer(:tbClientName) to customer.tbClientName is not solution.
Some help would be greatly appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,首先:我认为您实际上不想将所有这些列组合成一个字段和一个属性?
因此,删除
[]
- 您可以传入多个列,但如果您传入显式数组,那么会将这些列分组到单个字段/属性中。因此,也删除:as
选项 - 您不希望所有这些列具有相同的名称,生成的 SQL 语句将没有任何意义。其次:属性本质上是可排序的,因此您不需要传递
:sortable =>; true
到has
调用。另外:您真的需要所有字段都可以排序吗?最好只标记应该可排序的字段。但最后,也许是最重要的:实际发生的错误是因为您的两个关联的条件散列所致。您在同一个表上加入两次,但条件中没有任何表引用。尝试将两个条件设置更改为以下内容:
Well, firstly: I don't think you actually want to combine all those columns into just one field and just one attribute?
So, remove the
[]
- you can pass in multiple columns, but if you pass in an explicit array, then that'll group those columns together into a single field/attribute. Thus, remove the:as
option as well - you don't want all of those columns together having the same name, the generated SQL statement won't make any sense.Secondly: attributes are sortable by their very nature, so you don't need to pass
:sortable => true
to thehas
call. Also: do you really need all of the fields to be sortable? Better to only mark the fields that should be sortable as such.But lastly, and perhaps most importantly: the error that's actually happening is because of your conditions hash for your two associations. You're joining on the same table twice, but you don't have any table references in the conditions. Try changing both conditions settings to the following: