Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何传入多个范围的范围_Ruby On Rails_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 如何传入多个范围的范围

Ruby on rails 如何传入多个范围的范围,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有一个Courier对象,我希望能够致电Courier.daily_orders查找当天分配给Courier的所有订单 我有下面的许多范围不起作用 has_many :daily_orders, -> { where('orders.bucket_time >= ? AND orders.bucket_time <= ?', courier.service_region.to_local_timezone(Time.now.beginning_of_day),

我有一个Courier对象,我希望能够致电Courier.daily_orders查找当天分配给Courier的所有订单

我有下面的许多范围不起作用

has_many :daily_orders, -> {
  where('orders.bucket_time >= ? AND orders.bucket_time <= ?',
    courier.service_region.to_local_timezone(Time.now.beginning_of_day),   
    courier.service_region.to_local_timezone(Time.now.end_of_day)) },
  class_name: Order
如何传入courier对象以便调用courier.service\u region…?

您可以使用此

has_many :daily_orders, -> (courier) { where('...') }
或者是lambda的详细版本

或程序


尽管我的回答可能会解决你的问题,但我一直在考虑你的设计。我猜快递员有很多:订单,如果是,为什么不为每日订单创建一个范围?
has_many :daily_orders, lambda { |courier| where('...') }
has_many :daily_orders, proc { |courier| where('...') }