Note that there are some explanatory texts on larger screens.

plurals
  1. POhas_one :conditions being interpreted as attributes?
    primarykey
    data
    text
    <p>This association:</p> <pre><code>class Business &lt; ActiveRecord::Base has_one :map_address, :as=&gt;:addressable, :class_name =&gt; 'Address', :conditions =&gt; {'addresses.map_address'=&gt;true}, :dependent =&gt; :destroy end </code></pre> <p>Causes this error:</p> <pre><code>ruby-1.8.7-p334 :005 &gt; n = Business.new ruby-1.8.7-p334 :006 &gt; n.build_map_address ActiveRecord::UnknownAttributeError: unknown attribute: addresses.map_address </code></pre> <p>The code used to read </p> <pre><code>:conditions =&gt; {:map_address=&gt;true} </code></pre> <p>But the lack of table name causes this issue on lookup (it's putting the field name on the wrong table): </p> <pre><code>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): </code></pre> <p>This option:</p> <pre><code>"adresses.map_address is true" </code></pre> <p>produces this error on lookup:</p> <pre><code>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 </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload