Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to design model with two, independent and optional relations to the same model?
    primarykey
    data
    text
    <p>I've got a problem with designing my <code>User</code> model and making a decent form for it. I just want to ensure myself that I'm doing it wrong :) So it goes like this:</p> <p><code>User</code> has got two <code>Address</code>es: </p> <ul> <li>a mandatory <code>Address</code> for identification and billing,</li> <li>an optional shipping <code>Address</code> that he could fill in or leave blank</li> </ul> <p>I tried like this:</p> <pre><code>class User &lt; ActiveRecord::Base has_one :address has_one :shipping_address, :class_name =&gt; 'Address', :foreign_key =&gt; 'shipping_address_id' accepts_nested_attributes_for :address accepts_nested_attributes_for :shipping_address #validations for user end </code></pre> <p>and:</p> <pre><code>class Address &lt; ActiveRecord::Base #validations for address end </code></pre> <p>And then I make a form for <code>User</code> using <code>form_for</code> and nested <code>fields_for</code>. Like this:</p> <pre><code>= form_for @user, :url =&gt; '...' do |a| = f.error_messages ... = fields_for :address, @user.build_address do |a| ... </code></pre> <p>But then, despite that <code>f.error_messages</code> generates errors for all models, fields for <code>Address</code>es don't highlight when wrong.</p> <p>Also I have problems with disabling validation of the second address when the user chose not to fill it in.</p> <p>And I have doubts that my approach is correct. I mean the <code>has_one</code> relation and overall design of this contraption. </p> <p>So the question:</p> <p>Am I doing it wrong? How would You do that in my place?</p>
    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