Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple nested attributes of the same type
    text
    copied!<p>I have two models - "symbols" and "users". Among other attributes, symbols has "created_by_id" and "updated_by_id" attributes which are id's of users that created/updated a certain symbol entry. </p> <p>Let's say I want to display the symbols table with their "symbol" attribute and the nested "created by" and "updated by" (user.username) attributes for each symbol entry. Resulting table should look something like this:</p> <pre><code>symbol created_by updated_by ------------------------------------ symbol1 username1 username2 symbol2 username1 username2 </code></pre> <p>How can I achieve this? I guess I need <code>accepts_nested_attributes_for :user</code> and probably <code>has_one :user</code> (?) in my Symbol model. Do I also need <code>belongs_to :user</code> in my User model? </p> <p>After the models are set up properly, how can I access the username of users associated with "created_by_id" and "updated_by_id" in my view?</p> <p>I have an edit form where I used nested form like this (which works fine):</p> <pre><code>&lt;%= form_for @symbol do |f| %&gt; Symbol: &lt;%= f.text_field :symbol %&gt; &lt;%= f.fields_for :kanji do |kf| %&gt; Meaning: &lt;%= kf.text_field :meaning %&gt; Onyomi: &lt;%= kf.text_field :onyomi %&gt; Kunyomi: &lt;%= kf.text_field :kunyomi %&gt; &lt;% end %&gt; &lt;%= f.submit "Save" %&gt; &lt;% end %&gt; </code></pre> <p>but I couldn't figure out how to do something similar in this case where I have two nested attributes associated with the same symbol.</p> <p>I'm new to rails so perhaps I got the whole idea of how to do this wrong. If there is a better than what I just explained how I want to do it, please correct me.</p> <p>edit: Here are the models. The first one is actually called JSymbol but the problem remains the same.</p> <pre><code>class JSymbol &lt; ActiveRecord::Base belongs_to :j_symbol_type has_one :kanji, :dependent =&gt; :destroy has_one :radical, :dependent =&gt; :destroy has_one :vocabulary, :dependent =&gt; :destroy attr_accessible :created_by_id, :updated_by_id, :symbol, :j_symbol_type_id, :level attr_accessible :kanji_attributes, :radical_attributes, :vocabulary_attributes accepts_nested_attributes_for :kanji, :radical, :vocabulary end </code></pre> <p>And the Users model.</p> <pre><code>class User &lt; ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable, :timeoutable attr_accessible :username, :email, :password, :password_confirmation, :remember_me validates :username, :presence =&gt; true, :uniqueness =&gt; true end </code></pre> <p>This is of course without the user-jsymbol relation since I am not sure what kind of relation to add in there.. It should be something like <code>has_one :user</code> in JSymbol model and <code>belongs_to :j_symbol</code> in User model except it needs to be done twice somehow.. There is also the matter of calling .build method in the action method for each Symbol.</p>
 

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