我有三个模型:
User (first_name, last_name, mail)
Article (name, info)
Article_Author (user_id and article_id)
在Article_controller
中,create
动作如下所示:
def create
@article = article.new(params[:article])
@article_author = @article.article_authors.build(params[:article_authors])
@article_authors.user_id = current_user.id
respond_to do |format|
if @article.save
format.html { redirect_to @article, :flash=>{:notice=>'Article was successfully created.'} }
else
format.html { render action: "new" }
end
end
end
看起来没问题,因为在Article_Author
表中我可以看到它user_id
等于当前用户 ID,而article_id
是当前文章 ID。在视图中,当我编写 时<%= @article.article_authors %>
,它给了我整个用户数组:[#<ArticleAuthors id: 1, user_id: 2, article_id: 10, created_at: "2013-01-15 18:46:39", updated_at: "2013-01-15 18:46:39">]
.
我怎样才能得到名字和姓氏?