2011-08-29 54 views
2

该协会:has_one:条件被解释为属性?

class Business < ActiveRecord::Base 
    has_one :map_address, :as=>:addressable, :class_name => 'Address', :conditions => {'addresses.map_address'=>true}, :dependent => :destroy 
end 

导致此错误:

ruby-1.8.7-p334 :005 > n = Business.new 
ruby-1.8.7-p334 :006 > n.build_map_address 
ActiveRecord::UnknownAttributeError: unknown attribute: addresses.map_address 

用来读取

:conditions => {:map_address=>true} 

但缺少表名的代码导致上查找这个问题(它把字段名称在错误的表格中):

ActiveRecord::StatementInvalid (PGError: ERROR: column counties.map_address does not exist 
                  ^
SELECT "businesses".* FROM "businesses" INNER JOIN "addresses" ON ("businesses"."id" = "addresses"."addressable_id" AND "addresses"."addressable_type" = 'Business') INNER JOIN "counties" ON ("counties"."id" = "addresses"."county_id") AND counties."map_address" IS NULL WHERE (counties.id = 23) ORDER BY businesses.updated_at DESC): 

此选项:

"adresses.map_address is true" 

生产上查找此错误:

PGError: ERROR: missing FROM-clause entry for table "adresses" 
LINE 1: ..." ON ("states"."id" = "addresses"."state_id") AND adresses.m... 
                  ^
: SELECT "businesses".* FROM "businesses" INNER JOIN "addresses" ON ("businesses"."id" = "addresses"."addressable_id" AND "addresses"."addressable_type" = 'Business') INNER JOIN "states" ON ("states"."id" = "addresses"."state_id") AND adresses.map_address is NULL WHERE (states.id = 4) ORDER BY businesses.updated_at DESC 

所以我的问题是,为什么在轨试图把这种状态变成一个属性?我怎样才能使它工作?我的猜测是rails试图将条件设置为新记录的默认值。

+0

好吧,没关系,我发现,表名的误解,实际上从searchlogic到来,所以这不是轨道的问题,毕竟。 :( – pixelearth

+0

那么正确的代码是什么样的? – Intentss

回答

0

是map_attribute一个字段?在这种情况下,有一个错误,因为你有address.map_address,但是你在说什么地址对象?通过写入地址而不是引用特定地址
换句话说,Map_address似乎是一个对象的属性,因此object1具有其map_address,object2具有其map_address,但是当您键入地址时,不会引用任何对象,并且它会导致错误。

+0

4.3.2.4:条件 :conditions选项让你指定关联对象必须符合的条件(在SQL WHERE子句中使用的语法中)。地址是一个表名 – pixelearth

+0

确认{:addresses => {:map_address => true}}也不起作用 – pixelearth

0

为什么不

has_one :map_address, :as=>:addressable, :class_name => 'Address', :conditions => {:map_address => true}, :dependent => :destroy 
+0

这就是它最初的内容,请参阅问题 – pixelearth