开发者

Finder Methods Related Controllers

I have two controllers, orders and tasks - each order has many tasks.

In my orders show view, I list all the related tasks for the order.

My issue is that I want to restrict the output - eg. only list tasks with a status = 1

In my orders controller I tried this:

 @tasks = Task.find(:all, :conditions => [:status => '1'])

But I get an error:

undefined method `%' for {:status=>"1"}:Hash

I also don't really know how to call this. In my orders show view I tried this:

<% @task.tasks.each do |task| %>
  <li>
    <%= task.title %> <%= task.duedda开发者_运维技巧te %> 
  </li>
<% end %>

Can you help a flailing newbie :)


You really need to check query interface in rails 3

http://railscasts.com/episodes/202-active-record-queries-in-rails-3

http://railscasts.com/episodes/215-advanced-queries-in-rails-3

Btwn your solution is:-

 @tasks = Task.where(['status = ? ', 0])


This is how I would approach this.

Task Model

def Task
    belongs_to :order

    scope :status, lambda { |stat| where(:status => stat) }
end

OrdersController

def show
    @order = Order.find(params[:id])
    @starting_tasks = @order.tasks.status(1).all
end

View /orders/show.html.erb

<%= @starting_tasks.each do |task| %>
    <li><%= task.title %> <%= task.dueddate %></li>
<% end %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜