Note that there are some explanatory texts on larger screens.

plurals
  1. PORails validations are not being run on nested model
    primarykey
    data
    text
    <p>I'm on Rails 3.2.8 and Ruby 1.9.3.</p> <p>I'm having trouble figuring out why the validations on the nested attributes are not being run or returning any errors. When I submit the form with nothing filled in, I get errors back for the parent model (User), but not for the child model (Account).</p> <p>In my code below, I have a User model which has_one owned_account (Account model), and an Account model that belongs_to an owner (User model). The Account model has a text field for a subdomain string.</p> <p>It seems that when I submit the form without including the subdomain field, the validations on the Account model are not run at all. Any ideas on how I can get the validations here working? Thanks in advance for any help or pointers.</p> <p><strong>user.rb</strong></p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :owned_account_attributes has_one :owned_account, :class_name =&gt; 'Account', :foreign_key =&gt; 'owner_id' validates_associated :owned_account accepts_nested_attributes_for :owned_account, :reject_if =&gt; proc { |attributes| attributes['subdomain'].blank? } end </code></pre> <p><strong>account.rb</strong></p> <pre><code>class Account &lt; ActiveRecord::Base attr_accessible :owner_id, :subdomain belongs_to :owner, :class_name =&gt; 'User' validates :subdomain, :presence =&gt; true, :uniqueness =&gt; true, :format =&gt; { ...some code... } end </code></pre> <p><strong>new.haml</strong></p> <pre><code>= form_for @user do |f| ... User related fields ... = f.fields_for :owned_account_attributes do |acct| = acct.label :subdomain = acct.text_field :subdomain = submit_tag ... </code></pre> <p><strong>users_controller.rb</strong></p> <pre><code>class UsersController &lt; ApplicationController def new @user = User.new end def create @user = User.new(params[:user]) if @user.save ... end end </code></pre>
    singulars
    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. 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