Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails saving multiple instances of a class using a single form for a different class
    primarykey
    data
    text
    <p>As a bit of background, I am making a system where registered users can post bounties for artwork. They say what they want and post it as a bounty to be publicly visible. Users may also be registered into the system as artists.</p> <p>But the trick is that the user posting the bounty is allowed to specify a subset of registered artists that are allowed to accept the bounty. I'd guess what I need to make the bounty via a form is the helpful <code>form_for</code> tool...</p> <pre><code>&lt;%= form_for @bounty do |bounty_form| %&gt; &lt;div class="field"&gt; &lt;%= bounty_form.label :name %&gt; &lt;%= bounty_form.text_field :name %&gt; &lt;/div&gt; &lt;div class="field"&gt; &lt;%= bounty_form.label :desc %&gt; &lt;%= bounty_form.text_area :desc %&gt; &lt;/div&gt; ... </code></pre> <p>Saving a new instance of the Bounty class this way is easy. But the problem is that I also want to save multiple instances of a Candidacies class along with this, depending on which artists the user selects (via checkboxes) when saving this bounty. So say there are only 2 artists in the system, Artist1 and Artist2, the user should have the ability to select 1, 2, neither, or both and it should create the candidacies along with the bounty.</p> <p>I'm aware of <code>accepts_nested_attributes_for</code>, but it seems that it's useful for creating single instances of classes, like making an address object when you save a person object. What I need is a means to save multiple (0-n) classes on a single form submit.</p> <p>Here's some reference : </p> <p>Bounties are just names, descriptions, price... things like that. It's this table the form_for is originally being created for.</p> <pre><code># == Schema Information # # Table name: bounties # # id :integer not null, primary key # name :string(255) not null # desc :text not null # price_cents :integer default(0), not null # price_currency :string(255) default("USD"), not null # rating :boolean default(FALSE), not null # private :boolean default(FALSE), not null # url :string(255) # user_id :integer not null # accept_id :integer # reject_id :integer # complete_id :integer # created_at :datetime not null # updated_at :datetime not null # </code></pre> <p>And then it is this small many-to-many join table that needs to be populated when bounty is saved, depending on the user's submission.</p> <pre><code># == Schema Information # # Table name: candidacies # # id :integer not null, primary key # user_id :integer not null # bounty_id :integer not null # created_at :datetime not null # updated_at :datetime not null # class Candidacy &lt; ActiveRecord::Base attr_protected :id, :user_id, :bounty_id #Many to many join table between user and bounty. belongs_to :user belongs_to :bounty validates :user_id, presence: true validates :bounty_id, presence: true end </code></pre> <p>Lastly, the artists in the system are supplied by an <code>@artist</code> instance variable.</p> <p>In summary : I need to be able to save (0-n) candidacies along with a single save of a bounty, preferably using form_for.</p> <p>I am very new to rails and programming in general. Like many, I am learning rails as my first foray into development and I appreciate that there are communities like this that are around to help. Thank you in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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