1

I have a form which creates a Listing - but I want it to create some associated records. My models are structured as follows:

  • Listing has_and_belongs_to_many Cards (as in business cards)
  • Card belongs_to Company (a company can have many business cards)

When I submit this form I want to create a Listing, a Card and a Company in one shot. Also validations should be run.

Now I understand that if I want to include a Card field in my Listing form I'll use something like:

@card = @listing.cards.build
[...]
fields_for(@listing, @card) do |c|

But what should I do if I want to include Company fields in the form? I checked, but @card.company is nil.

2 Answers 2

1

Short answer:

building a card doesn't automatically create an associated company

Long answer

The most reasonable way IMHO would be to start with the company and create associations around it even if you want to create a listing.

See it this way:

  • listing and company can exist on their own
  • card presupposes the existence of a company
  • a listing and a card are associated through a join table

Ergo: if you want to create a listing that has an associated card, you would also need a company to which that card belongs. Thus we would put the company on top of the hierarchy and do something like this:

class CompanyController < ApplicationController
  ...
  def new
    @company = Company.new
    @card = @company.cards.build
    @listing = @card.listings.build
  end
  ...
end

Respectively in your nested form you will have the company at the top, card next and listing at last:

= form_for @company do |company_f|
  - # company stuff
  = company_f.fields_for @card do |card_f|
    - # card stuff
    = card_f.fields_for @listing do |listing_f|
      - # listing stuff
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this was what I was looking for. Didn't think about it from this perspective. I was thinking that maybe you could suggest a better title for the question?
@kitsched I am really flattered. Maybe a title like "initiating and creating multiple associated records in a single form" suits the question? What do you say?
0

You should use fields_for form helper http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

Or use gems like https://github.com/ryanb/nested_form

8 Comments

I use fields_for, but the problem is that the Company resource is nil.
If a company is defined, just use its id. If it is not there use company.build also
or you can have a select_tag for company select.
That's the problem, it is not, basically I want to create a Listing, Card and Company in one shot.
use company.build, and add more additional attributes for if wit fields_for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.