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 试图将这种条件转变为属性?我怎样才能让它双向发挥作用?我的猜测是,rails 正在尝试将条件设置为新记录的默认值。
This association:
class Business < ActiveRecord::Base
has_one :map_address, :as=>:addressable, :class_name => 'Address', :conditions => {'addresses.map_address'=>true}, :dependent => :destroy
end
Causes this error:
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
The code used to read
:conditions => {:map_address=>true}
But the lack of table name causes this issue on lookup (it's putting the field name on the wrong table):
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):
This option:
"adresses.map_address is true"
produces this error on lookup:
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
So my question is why is rails trying to turn this condition into an attribute? And how can I make it work both ways? My guess is that rails is trying to set the condition up as a default value for the new record.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不呢
why not
map_attribute 是一个字段吗?在这种情况下,会出现错误,因为您有addresses.map_address,但您在谈论什么地址对象?通过编写地址,您并不是引用特定的地址
换句话说,Map_address似乎是对象的属性,因此object1有它的map_address,object2有它的map_address,但是当您键入地址时,您并没有引用任何对象,并且会导致错误。
Is map_attribute a field ? In such case there is an error because you have addresses.map_address, but what addresses object are you talking about ? by writing addresses you are not refering to a specific address
In other words Map_address seems to be an attribute of an object, so object1 has its map_address, object2 has its map_address, but when you type addresses you are not referring to any object, and it causes an error.