Multiple nested attributes of the same type

14 views
Skip to first unread message

pootzko cmik

unread,
Jun 30, 2013, 2:56:59 PM6/30/13
to rubyonra...@googlegroups.com
Hi all,

0 down vote favorite


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.

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:

symbol created_by updated_by
------------------------------------
symbol1 username1 username2
symbol2 username1 username2

How can I achieve this? I guess I need accepts_nested_attributes_for
:user and probably has_one :user (?) in my Symbol model. Do I also need
belongs_to :user in my User model?

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?

I have an edit form where I used nested form like this (which works
fine):

<%= form_for @symbol do |f| %>
Symbol:
<%= f.text_field :symbol %>
<%= f.fields_for :kanji do |kf| %>
Meaning:
<%= kf.text_field :meaning %>

Onyomi:
<%= kf.text_field :onyomi %>

Kunyomi:
<%= kf.text_field :kunyomi %>
<% end %>
<%= f.submit "Save" %>
<% end %>

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.

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.

Thank you

--
Posted via http://www.ruby-forum.com/.

Rick

unread,
Jul 1, 2013, 2:15:56 PM7/1/13
to rubyonra...@googlegroups.com

Colin Law

unread,
Jul 2, 2013, 3:13:09 AM7/2/13
to rubyonra...@googlegroups.com
On 1 July 2013 19:15, Rick <richard...@gmail.com> wrote:
> the answer is to be found here:
> http://guides.rubyonrails.org/association_basics.html

Also work right through a good tutorial such as railstutorial.org
(which is free to use online). This will show you the basics of
Rails.

Colin
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/266afa94-daf1-479b-abda-a83e637d5a93%40googlegroups.com.
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages