Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The validations do run from the inherited model.</p> <p>Here's a test case showing that it works.</p> <p>Test case migration:</p> <pre><code>class CreateUser &lt; ActiveRecord::Migration def self.up create_table :users do |t| t.column :name,:string t.column :email, :string t.timestamps end end def self.down drop_table :users end end </code></pre> <p>Test Models:</p> <pre><code>class User &lt; ActiveRecord::Base validates_presence_of :name end class Sub &lt; User validates_presence_of :email end </code></pre> <p>Tests Cases:</p> <p>Creating a sub without either a name or email, save should fail with name,email can't be blank</p> <pre><code>&gt;&gt; b = Sub.create() =&gt; #&lt;Sub id: nil, name: nil, email: nil, created_at: nil, updated_at: nil&gt; &gt;&gt; b.save =&gt; false &gt;&gt; b.errors =&gt; #&lt;ActiveRecord::Errors:0x2457458 @errors={"name"=&gt;["can't be blank"], "email"=&gt;["can't be blank"]}, @base=#&lt;Sub id: nil, name: nil, email: nil, created_at: nil, updated_at: nil&gt;&gt; </code></pre> <p>Creating a sub with no email, save should fail with email can't be blank.</p> <pre><code>&gt;&gt; b = Sub.create(:name=&gt;'test') =&gt; #&lt;Sub id: nil, name: "test", email: nil, created_at: nil, updated_at: nil&gt; &gt;&gt; b.save =&gt; false &gt;&gt; b.errors =&gt; #&lt;ActiveRecord::Errors:0x243865c @errors={"email"=&gt;["can't be blank"]}, @base=#&lt;Sub id: nil, name: "test", email: nil, created_at: nil, updated_at: nil&gt;&gt; </code></pre> <p>Creating a sub with no name, save should fail with name can't be blank.</p> <pre><code>&gt;&gt; b = Sub.create(:email=&gt;'test') =&gt; #&lt;Sub id: nil, name: nil, email: "test", created_at: nil, updated_at: nil&gt; &gt;&gt; b.save =&gt; false &gt;&gt; b.errors =&gt; #&lt;ActiveRecord::Errors:0x2429594 @errors={"name"=&gt;["can't be blank"]}, @base=#&lt;Sub id: nil, name: nil, email: "test", created_at: nil, updated_at: nil&gt;&gt; </code></pre> <p>Creating a sub with name and email, save should succeed.</p> <pre><code>&gt;&gt; b = Sub.create(:email=&gt;'test',:name=&gt;'test') =&gt; #&lt;Sub id: 4, name: "test", email: "test", created_at: "2009-10-15 22:27:53", updated_at: "2009-10-15 22:27:53"&gt; &gt;&gt; b.save =&gt; true </code></pre>
 

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