此协会:
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试图将这个条件变成一个属性?我怎样才能使它双向工作?我的猜测是rails试图将条件设置为新记录的默认值。
答案 0 :(得分:0)
map_attribute是一个字段吗?在这种情况下,有一个错误,因为你有addresses.map_address,但是你在说什么地址对象?通过写地址你没有引用特定的地址
换句话说,Map_address似乎是一个对象的属性,所以object1有它的map_address,object2有它的map_address,但是当你输入地址时你不是指任何地址对象,它会导致错误。
答案 1 :(得分:0)
为什么不
has_one :map_address, :as=>:addressable, :class_name => 'Address', :conditions => {:map_address => true}, :dependent => :destroy