1

有兩個型號的記錄一些月度統計數據,我嘗試使用強制執行模型中的複合鍵約束:Rails 3 validates_uniqueness_of with:scope生成無效的SQL?

validates_uniqueness_of :entity_id, :scope => [:year, :month] 

當試圖運行記錄一個.valid?方法,我不斷地得到一個錯誤的顯然ActiveRecord正在生成不正確的SQL。

SQLite3::SQLException: near "FROM": syntax error: SELECT  FROM  "table" WHERE  ("table"."entity_id" = 1) AND ("table"."year" = 2007) AND ("table"."month" = 6) LIMIT 1 

注意上面說Rails沒有在select語句中添加*,所以SQLite正確地拋出一個錯誤。

任何想法,如果我在這裏做錯了什麼?

回答

2

試試這個

validates :entity_id, :uniqueness => {:scope => [:year, :month]} 

你可能想

:presence => true