[英]Why does this :id in Rails not work with Postgresql but it does work with MySQL?
我在Rails中將這種方法用於搜索引擎友好的URL- http://jroller.com/obie/entry/seo_optimization_of_urls_in
我的模型看起來像這樣。
class Listing < ActiveRecord::Base
def to_param
"#{id}-#{title.parameterize}"
end
end
這適用於MySQL,但不適用於Postgresql。
這是我得到的錯誤:
ActiveRecord :: StatementInvalid(PGError:錯誤:整數的無效輸入語法:“ 16- College-Station-apartments”:SELECT * FROM“ floorplans” WHERE(“ floorplans”。“ listing_id” = E'16-college-station-apartments ')ORDER BY床ASC,浴室ASC,鏡頭ASC,價格ASC):
ActiveRecord是否在Postgresql的查找中不執行.to_i?
Rails會針對某些方法自動在參數上調用to_i
,主要是那些期望將整數作為參數的方法,例如Listing.find(params[:id])
。
但是,對於可以接受字符串作為參數的其他類型的搜索方法,您需要手動調用to_i
Listing.find_by_id(params[:id].to_i)
Listing.find(:conditions => ["id = ?", params[:id].to_i])
您對MySQL沒問題的原因是MySQL to_i
在其末尾執行了to_i
(即,這不是數據庫適配器問題,而是實際數據庫服務器的功能)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.