1

我期待設置一個與它關聯的數據類型的attribute_accessor,就像我們如何在模型中有字段的數據庫具有與它們相關的字符串,整數等數據類型一樣。屬性訪問器的數據類型

我該如何告訴rails我的attr_accessor應該響應特定的數據類型。我期待設置一個日期時間到一個我試圖創建的訪問器,

請建議。

回答

3

你可以像

def field_name=(value) 
    # check for type and dont assign it if its the wrong typ 
    # or raise an error 
end 

一個定製的setter,就在我的頭頂的

此外,重複的問題:attr_accessor strongly typed Ruby on Rails

+0

attr_accessible和attr_accessor是兩個完全不同的事情,attr_accessible有與上面提到的問題無關。 – 2012-07-26 18:32:16

+0

哎呀,編輯答案 – AJcodez 2012-07-26 18:33:23