Note that there are some explanatory texts on larger screens.

plurals
  1. POComplex forms - Managing multiple models in a single form - (3rd degree madness)
    primarykey
    data
    text
    <p><strong>I have isolated the problem in the second block of code below</strong> (if you don't want all the details): I can create new users from the account model. I can't assign those users roles from the accounts model. I am using fields_for, this method does not work when I attempt to assign the role_ids to the roles model. My db is set up in the following way:</p> <ol> <li><p>Account model has_many :<strong>users</strong></p></li> <li><p>User model has_and_belongs_to_many :<strong>roles</strong>, belongs_to :<strong>accounts</strong></p></li> <li><p>Roles model has_and_belongs_to_many :<strong>users</strong></p></li> </ol> <p>views/accounts/new.html.erb </p> <pre><code>&lt;% for user in @account.users %&gt; &lt;% fields_for "account[user_attributes][]", user do |account_fields| %&gt; &lt;p&gt;Login : &lt;%= account_fields.text_field :login %&gt; &lt;p&gt;Email : &lt;%= account_fields.text_field :email %&gt; &lt;p&gt;Password : &lt;%= account_fields.password_field :password %&gt; &lt;p&gt;Confirm Password : &lt;%= account_fields.password_field :password_confirmation %&gt; &lt;%= account_fields.hidden_field :account_id, :value =&gt; :id %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% for role in @account.users.first.roles %&gt; &lt;% fields_for "account[role_attributes]]", role do |role_fields| %&gt; &lt;%= role_fields.hidden_field :role_ids, :value =&gt; '[1]' %&gt; &lt;% end %&gt; &lt;% end%&gt; </code></pre> <p>Associated setter methods in the account.rb model: using the stacktrace I have isolated the problem to "undefined method `roles' for #" on line 34, marked below:</p> <pre><code>def user_attributes=(user_attributes) user_attributes.each do |attributes| users.build(attributes) end end def role_attributes=(role_attributes) role_attributes.each do |attributes| users.roles.build(attributes) #error here (stacktrace) end end </code></pre> <p>finally, within the accounts controller I build the users and roles in memory:</p> <pre><code> def new @account = Account.new 1.times { @account.users.build } 1.times { @account.users.first.roles.build } </code></pre> <p>The @accounts.users.first.roles.build proof from consol: </p> <pre><code>&gt;&gt; account =&gt; #&lt;Account id: 1, subdomain: "justinzollars", created_at: "2010-02-08 14:41:13", updated_at: "2010-02-08 14:41:13"&gt; &gt;&gt; account.users =&gt; [#&lt;User id: 13, login: "jayz", email: "jz@jz.com", crypted_password: "f9a3d618fc650d285a90f9775508c13784891b97", salt: "f497a7dd909b695caff1f6310e710245615d55b6", created_at: "2010-02-08 20:25:48", updated_at: "2010-02-08 20:25:48", remember_token: nil, remember_token_expires_at: nil, account_id: 1&gt;, #&lt;User id: 16, login: "jasonwade23", email: "jasonwade@gmail.com", crypted_password: "06581b47cfac7a529773d61dc0b1d5d6c0da6c08", salt: "93f8b99cd9da60b904d553fcc7843bfb66352c3e", created_at: "2010-02-13 07:46:14", updated_at: "2010-02-13 07:46:14", remember_token: nil, remember_token_expires_at: nil, account_id: 1&gt;] &gt;&gt; account.users.first =&gt; #&lt;User id: 13, login: "jayz", email: "jz@jz.com", crypted_password: "f9a3d618fc650d285a90f9775508c13784891b97", salt: "f497a7dd909b695caff1f6310e710245615d55b6", created_at: "2010-02-08 20:25:48", updated_at: "2010-02-08 20:25:48", remember_token: nil, remember_token_expires_at: nil, account_id: 1&gt; &gt;&gt; account.users.first.roles =&gt; [#&lt;Role id: 1, name: "admin"&gt;, #&lt;Role id: 2, name: "alt"&gt;] &gt;&gt; </code></pre>
    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.
 

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