the answer is to be found here: 
http://guides.rubyonrails.org/association_basics.html

On Sunday, June 30, 2013 2:56:59 PM UTC-4, Ruby-Forum.com User wrote:
>
> 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/. 
>

-- 
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-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@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 via email to