Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有两个模型:
User(id:integer name:string) and Task(id:integer task:string).
情景是:
如何仅向用户显示他已完成的任务?
我会这样:
在任务模型中创建一个名为 done 的方法
def done where(:status => 'done', :user = current_user) end
然后在任务控制器中执行@done = Task.done
然后在视图中
<%= @done.each do |one_done| %> <%= one_done.name %> <% end %>
您需要确保User has_many :tasks迁移Task belongs_to :user 已在Task (user_id).
User has_many :tasks
Task belongs_to :user
Task (user_id)