Note that there are some explanatory texts on larger screens.

plurals
  1. POSkip validation for some members in associated models during create/update
    primarykey
    data
    text
    <p>I have the following 4 models</p> <pre><code>Hotel (name) has_one :address has_one :contact has_one :bank_account validates_presence_of :name def build_dependencies build_contact build_address build_bank_account end Address (phone, street_address, hotel_id) belongs_to :hotel validates_presence_of :phone, :street_address Contact (name, email, hotel_id) belongs_to :hotel validates_presence_of :name, :email BankAccount (name, number, hotel_id) belongs_to :hotel validates_presence_of :name, :number </code></pre> <p>In a form used to create a Hotel, I take input for both name and email for the Contact model but only phone for the Address model.</p> <pre><code>HotelController#new @hotel = Hotel.new @hotel.build_dependencies #this creates empty Contact and Address to generate the form fields #render the form to create the hotel HotelController#create #receive form data @hotel = Hotel.new @hotel.build_dependencies @hotel.save :validate =&gt; false @hotel.attributes = params[:hotel] @hotel.save :validate =&gt; false </code></pre> <p>This is the only way I was able to create a Hotel with contact information, phone from address and empty bank account. I had to call </p> <pre><code>@hotel.save :validate =&gt; false </code></pre> <p>the first time to save the Hotel instance with blank instances of BankAccount, Address, Contact. Then I had to update_attributes on contact and address and then</p> <pre><code>@hotel.save :validate =&gt; false </code></pre> <p>to ensure that the original form data got saved completely as expected.</p> <p>This, beyond doubt, is a very bad piece of code. Can anyone tell me how to clean this up?</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.
    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