5
votes

I'm trying to display a list of milestones for a particular order. (Orders have many milestones.)

In my orders model, I have this:

scope :open, lambda { 
     joins("join milestones on milestones.order_id = orders.id").
      where("order_id = ? AND milestone_status = ?", :params[:order_id], true).
      group("orders.id")
    }

The problem I'm having is getting the current order ID to work - :params[:order_id] is clearly wrong.

In my routes I have this:

resources :orders do
     resources :milestones
  end

And my url is as follows:

http://127.0.0.1/orders/2/milestones

How is this possible? I have tested the scope by replacing with an order ID manually.

-- EDIT --

As per advice below, I've put the following in my milestones controller:

@orders = Order.open( params[:order_id] )

And in my view, I have this:

<% @orders.each do |open| %>

But I get an error:

wrong number of arguments (1 for 0)

The full stacktrace is here: http://pastie.org/2442518

1
Will do. Just popped out. Will paste later. Thanks againJenny Blunt
@Mauricio-Linhares. Have edited my answer with the full trace - thanks for the help.Jenny Blunt
What are you trying to get with this scope, the milestones of the order order_id which have a true status? Your intent isn't clear from your question, it would really help to have a better idea of what you're aiming for.Benoit Garret
Did you get it worked at the end? I have the same issue: stackoverflow.com/questions/19667615/…John Smith

1 Answers

4
votes

Define it like this:

scope :open, lambda { |order_id|
 joins("join milestones on milestones.order_id = orders.id").
  where("order_id = ? AND milestone_status = ?", order_id, true).
  group("orders.id")
}

And call it on your controller like this:

def index
    @orders = Order.open( params[:order_id] )
end