2015-10-27 33 views

回答

1

如果A型是User和B型是Post和用戶has_many職位,那麼你將能夠訪問用戶的帖子,像這樣:

# In controller 
@user = User.find(params[:id]) 

# In view 

# if user has_many posts 
<% @user.posts.each do |post| %> 
    <%= link_to 'Edit', edit_post_path(post) %> 
<% end %> 

# if user has_one post 
<%= link_to 'Edit', edit_post_path(@user.post) %> 

這可能不反映你的模型雖然如此,如果不作評論。

你應該看看the rails association guide或更深入的瞭解做rails tutorial by Michael Hartl。這很棒。

相關問題