Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckboxes to select multiple records and create new association records if not exist
    text
    copied!<p>I have the following models (user, review, and period). What I would like to do is to have checkboxes in <strong>user index view</strong> where I can select multiple users (or all) and click a "Create Review" button where upon clicking, it will:</p> <ol> <li>Check if the selected users have a <strong>review</strong> record and if not create one for all selected users.</li> <li>Also this will also update the review record with the period_id of '1'</li> </ol> <p>I'm not sure on how to go about this and any help is much appreciated.</p> <p>Thank you in advance.</p> <p>user.rb</p> <pre><code>class User &lt; ActiveRecord::Base belongs_to :admin has_many :reviews has_many :periods, :through =&gt; :reviews devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :role end </code></pre> <p>review.rb</p> <pre><code>class Review &lt; ActiveRecord::Base belongs_to :user belongs_to :period attr_accessible :employee_comments, :employee_final_comments, :manager_comments, :period_id, :rating, :review_status, :user_id end </code></pre> <p>period.rb</p> <pre><code>class Period &lt; ActiveRecord::Base belongs_to :admin has_many :reviews has_many :users, :through =&gt; :reviews attr_accessible :admin_id, :end_date, :period_name, :start_date, :active end </code></pre> <p><strong>Update 1</strong></p> <p>I've added the following lines into my index.html.erb (based on railscast <a href="http://railscasts.com/episodes/165-edit-multiple" rel="nofollow">http://railscasts.com/episodes/165-edit-multiple</a>)), but getting a blank list. If I remove the form_tag line, I can see the list of users with the checkboxes.</p> <p>app/views/users/index.html.erb</p> <pre><code>&lt;% form_tag create_multiple_users_path do %&gt; . . . &lt;th&gt;&lt;%= check_box_tag "users_ids[]", user.id %&gt;&lt;/th&gt; &lt;% end %&gt; . . . &lt;%= submit_tag "Create Reviews for selected users" %&gt; </code></pre> <p>routes.rb</p> <pre><code>resources :users do collection do post 'create_multiple' end end </code></pre>
 

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