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.i开发者_JS百科d = 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.
why not
has_one :map_address, :as=>:addressable, :class_name => 'Address', :conditions => {:map_address => true}, :dependent => :destroy
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.
精彩评论