我希望是个简单的问题。
如何将这行named_scope代码从Rails2应用程序转换为Rails5的范围代码行
原创..。
named_scope :effective_on, lambda { |date|
{ :conditions => ['(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date] }
}我已经尝试过了,但它只是将条件行作为字符串打印出来...
scope :effective_on, lambda { |date|
{ :conditions => ['(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date] }
}我怀疑这是因为Rails 5.0不推荐使用"conditions“,但当我试图在这个版本中用"where”替换它时,它在我面前爆炸了……
scope :effective_on, lambda { |date|
{ where('(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date) }
}..。整个"where“行在我的集成开发环境中亮起红色,它告诉我"Expected:=>”
这就是我被难住的地方。
发布于 2021-10-17 00:27:00
问题在于,旧版Rails中的作用域返回了类似{ :conditions => 'some conditions }的散列,但在较新版本中,它返回了活动记录关系(类似于where方法的返回值)。
所以你必须改变:
scope :effective_on, lambda { |date|
{ :conditions => ['(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date] }
}至
scope :effective_on, lambda { |date|
where('(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date)
}如果没有围绕where调用的{ }
https://stackoverflow.com/questions/69600394
复制相似问题