Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 How can I use :params in named scope?

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

like image 275
Jenny Blunt Avatar asked Oct 10 '22 04:10

Jenny Blunt


1 Answers

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
like image 151
Maurício Linhares Avatar answered Oct 14 '22 04:10

Maurício Linhares