1
我試圖建立我的條件,像這樣:導軌 - 建設條件語句
conditions = {}
conditions[:doors] = 4
conditions[:type] = "sedan"
Cars.find(:all, :conditions=>conditions)
我將如何添加到我的條件「喜歡」的條件:
conditions[:color] NOT LIKE '%black%'
我試圖建立我的條件,像這樣:導軌 - 建設條件語句
conditions = {}
conditions[:doors] = 4
conditions[:type] = "sedan"
Cars.find(:all, :conditions=>conditions)
我將如何添加到我的條件「喜歡」的條件:
conditions[:color] NOT LIKE '%black%'
如果你有滑軌3+考慮使用新的AREL語法爲您的某些查詢構建:
records = Car.where(:doors => 4, :type => "sedan")
records = records.where("color NOT LIKE ?", "%black%")
但是,如果您仍在使用Rails 2.3,那麼語法看起來像:
clauses = ["doors = ?", "type = ?"]
conditions = [4, 'sedan']
if some_reason
clauses << "color NOT LIKE ?"
conditions << "%robert%"
end
Cars.find(:all, :conditions => [clauses.join(" AND"), *conditions])
絕對比AREL語法更惱人。
該語法的一個副作用是當ActiveRecord替換?
時,它將清理SQL注入的輸入。