Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some research, I am ready to answer my own question. </p> <p>First, I have to say thanks to @Lichtamberg for suggesting the fix. However, that only complicates things (also regarding security, though not an issue in this case), and doesn't help me reach my ideal solution. </p> <p>Digging more, I found out that this is a very common scenario in Rails, and it's actually explained in <a href="http://railscasts.com/episodes/17-habtm-checkboxes" rel="noreferrer">Ryan Bates' screencast no #17</a>. </p> <p>Therefore, in Rails, if you have a has_and_belongs_to_many (short form HABTM) association, you can easily set the ids of the other associated object through this method:</p> <pre><code>profile.lifestyle_ids = [1,2] </code></pre> <p>And this obviously works for forms if you've set the attr_accessible for lifestyle_ids:</p> <pre><code>class Profile &lt; ActiveRecord::Base attr_accessible :lifestyle_ids end </code></pre> <p>In ActiveAdmin, because it uses <a href="http://rdoc.info/github/justinfrench/formtastic" rel="noreferrer">Formtastic</a>, you can use this method to output the correct fields (in this case checkboxes):</p> <pre><code>f.input :lifestyles, as: :check_boxes, collection: Lifestyle.all </code></pre> <p>Also, I have simplified my form view so it's now merely this:</p> <pre><code> form do |f| f.inputs # Include the default inputs f.inputs "Lifestlyes" do # Make a panel that holds inputs for lifestyles f.input :lifestyles, as: :check_boxes, collection: Lifestyle.all # Use formtastic to output my collection of checkboxes end f.actions # Include the default actions end </code></pre> <p>Ok, now this rendered perfectly in the view, but if I try and submit my changes, it gives me this database error:</p> <pre><code>PG::Error: ERROR: null value in column "created_at" violates not-null constraint : INSERT INTO "lifestyles_profiles" ("profile_id", "lifestyle_id") VALUES (2, 1) RETURNING "id" </code></pre> <p>I found out that this is due to the fact that Rails 3.2 doesn't automatically update the timestamps for a HABTM association table (because they are extra attributes, and Rails only handles the <code>_id</code> attributes.</p> <p>There are 2 solutions to fix this:</p> <ol> <li>Either convert the association into a hm:t (<code>has_many, :through =&gt;</code>)</li> <li>Or remove the timestamps from the table</li> </ol> <p>I'm going to go for 2) because I will never need the timestamps or any extra attributes.</p> <p>I hope this helps other people having the same problems. </p> <p><strong>Edit:</strong> @cdesrosiers was closest to the solution but I already wrote this answer before I read his. Anyway, this is great nevertheless. I'm learning a lot.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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