對於如何設置friendly_id(4.0.0.beta12)gem來正確使用STI模型,我非常難過。如何使用Friendly_Id w/STI?
這裏是模型設置:
class Car < ActiveRecord::Base
extend FriendlyId
friendly_id :name, :use => :slugged
end
class Ford < Car
end
class Toyota < Car
end
如果我嘗試這樣的事:
Toyota.create!(name: "test")
Ford.create!(name: "test")
產生的錯誤是:
(0.1ms) BEGIN
Ford Load (0.2ms) SELECT `cars`.* FROM `cars` WHERE `cars`.`type` IN ('Ford') AND (`slug` = 'test' OR `slug` LIKE 'test--%') AND (id <> 7606) ORDER BY LENGTH(`slug`) DESC, `slug` DESC LIMIT 1
(0.5ms) UPDATE `cars` SET `slug` = 'test', `updated_at` = '2011-09-30 15:06:08' WHERE `cars`.`type` IN ('Ford') AND `cars`.`id` = 7606
(0.1ms) ROLLBACK
ActiveRecord::RecordNotUnique: Mysql2::Error: Duplicate entry 'test' for key 2: UPDATE `cars` SET `slug` = 'test', `updated_at` = '2011-09-30 15:06:08' WHERE `cars`.`type` IN ('Ford') AND `cars`.`id` = 7606
的問題是,friendly_id的選擇容貌對於設置爲「福特」的類型的slu and,出現乾淨(因爲slu''測試'已經屬於記錄類型'豐田')。假設沒有名稱爲'test'的slu exists存在,它會嘗試用slu'測試'來保存記錄,所有事情都會下到地獄。
任何想法?
謝謝!
什麼版本friendly_id您使用的是? –
嗨納什,我使用friendly_id(4.0.0.beta12)(更新了這個問題)謝謝。 –
我應該補充一點,在我的gem文件中,我有'gem'friendly_id','〜> 4.0.0.beta8',但4.0.0.beta12是我安裝時運行'bundle install' –