Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While I've never seen this ActiveScaffold exception before, I think this is what's going on:</p> <p>The reverse association is set up with the <code>:inverse_of</code> option, and <strong>its effects will be visible only</strong> while the records are unsaved. This is why you don't see it in your console experiment.</p> <p>Try this:</p> <pre><code>place = Place.new =&gt; #&lt;Place ...&gt; producer = place.producers.build =&gt; #&lt;Producer ...&gt; producer.place =&gt; nil place.producers.first.place =&gt; nil </code></pre> <p>These records will fail your validation, when built entirely in memory. If you never build a place and a producer at the same time, then you don't have to worry about this. If you do, however, then your validation rule also should be written to check for the existence of <code>:place</code> (i.e. the object), not <code>:place_id</code>:</p> <pre><code>validates_presence_of :place </code></pre> <p>I don't know which build/creation mechanism ActiveScaffold uses, but I would guess that if they throw this exception, that they may build things in memory.</p> <p>So, to fix this, try:</p> <pre><code>class Producer &lt; ActiveRecord::Base belongs_to :place, inverse_of: producers validates_presence_of :place end class Place &lt; ActiveRecord::Base has_many :producers, inverse_of: place end </code></pre> <p>Note: ActiveRecord used to not support <code>:inverse_of</code> on the <code>has_many</code> side and that limitation used to be commented in the source. As of Rails 3.1, this seems fixed. If you amend your classes with <code>:inverse_of</code> as shown, then the console experiment with in-memory objects should return the associated object.</p> <p>You may want to review the Rails API docs on <a href="http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Bi-directional+associations" rel="nofollow">bi-directional associations</a></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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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