【发布时间】:2014-05-20 02:21:51
【问题描述】:
不确定如何正确设置这种关系。 3 个表:用户、帖子、状态
模型 -
User Model
has_many :posts
Post Model
belongs_to :user
has_one :status
Status Model
#Not sure about this.
表格——
Users: name, email, password_digest
Posts: user_id, status_id, post_title, post_content
Statuses: user_id, status_title, status_active
用户可以创建自己的状态,所以我不确定状态应该属于用户还是帖子。
在用户控制器中,对于新操作,我有以下帮助在创建新帖子时在下拉列表中生成状态选项:
def new
@post = Post.new
@options_statuses = Status.where(:admin_user_id => session[:id])
end
创建新帖子的代码(在部分中):
<%= f.label(:status_id, "Status") %><br/>
<%= f.select(:status_id, @options_statuses.map { |s| [ s.status_title, s.id ] }) %><br/><br/>
<%= f.label(:post_title, "Title") %><br/>
<%= f.text_field(:post_title) %><br/><br/>
<%= f.label(:post_content, "Content") %><br/>
<%= f.text_area(:post_content) %><br/><br/>
帖子信息已正确保存到数据库中。问题是当我想通过索引或显示操作显示帖子时。目前我有以下内容:
<% @posts.each do |post| %>
<%= post.status_id %> - <%= post.created_at %><br/>
<%= post.post_title %> - <%= post.post_content %>
<% end %>
我实际上不想显示 status_id,而是显示 status_title,但不知道该怎么做。
非常感谢任何帮助。
【问题讨论】:
标签: ruby-on-rails database activerecord ruby-on-rails-4 models