我希望的簡單問題。
如何將此命名范圍線從 rails 2 應用程式轉換為 rails 5 的范圍線
原來的...
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 不推薦使用“條件”,但是當我嘗試用此版本中的“位置”替換它時,它在我面前爆炸了......
scope :effective_on, lambda { |date|
{ where('(effective_on IS NULL OR effective_on <= ?) AND (ineffective_on IS NULL OR ineffective_on > ?)', date, date) }
}
...在我面前爆炸:整個“where”行在我的 IDE 中亮起紅色,它告訴我“預期:=>”
這就是我難倒的地方。
uj5u.com熱心網友回復:
問題是舊 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://www.uj5u.com/qukuanlian/328698.html
標籤:红宝石轨道 ruby-on-rails-5 ruby-on-rails-2
上一篇:Rails5:將find(:all,condition...)轉換為“where”
下一篇:Rails路由可選段映射問題
