0

我使用这样的范围:

# controller

    sales_rep = Car.sales_rep(branch_id)

# model - scope
  scope :sales_rep, -> (branch_id) { 
    find_by_sql("... a big, complicated query ...")
  }

到目前为止效果很好,但我需要添加另一个“过滤器”,例如:

sales_rep = Car.sales_rep(branch_id).by_year(params[:year]).by_year(params[:month])

我将附加范围添加到模型中:

  scope :by_year, (lambda do |year|
    if year.present?
       where('YEAR(s.delivery_date) = ?', year)
    end
  end)
  scope :by_month, (lambda do |month|
    if month.present?
       self.where('MONTH(s.delivery_date) = ?', month)
    end
  end)

当我运行此代码时,我收到此错误:

undefined method `by_year' for #<Array:0x007f8203f8a660>

如何接受find_by_sql另一个范围查询?

谢谢

4

0 回答 0