我有一個名爲start_date
的Time屬性模型。當我創建我的控制器一個新的對象和初始化的時間值,對象看起來是正確創建,當我在調試器中查看它...Rails 3:爲什麼我的Time屬性不能正確保存到數據庫?
(rdb:75) p @tourney
#<Tournament id: nil, created_at: nil, updated_at: nil, start_date: "2011-04-22 20:05:00", duration_in_mins: 30, winners_name: nil>
之後卻一直保存到數據庫中,start_date
價值是錯誤的,被設置爲默認值....
#<Tournament id: 5, created_at: "2011-04-22 20:07:56", updated_at: "2011-04-22 20:07:56", start_date: "2000-01-01 01:05:00", duration_in_mins: 30, winners_name: nil>
這裏是我的控制器代碼...
def create
@tourney = Tournament.new(params[:tournament])
@tourney.start_date = Time.new(params[:date][:year],
params[:date][:month],
params[:date][:day],
params[:date][:hour],
params[:date][:minute])
debugger
if @tourney.save
redirect_to dashboard_path, :notice => "Tournament Created Successfully"
else
redirect_to dashboard_path, :notice => "Tournament Creation Failed"
end
end
非常感謝你的智慧!
我的模型代碼...
# Table name: tournaments
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# start_date :time
# duration_in_mins :integer
# winners_name :string(255)
#
class Tournament < ActiveRecord::Base
has_many :participants, :dependent => :destroy
attr_accessible :start_date, :duration_in_mins
validates :start_date, :presence => true
end
顯示型號代碼 – fl00r 2011-04-22 20:19:05
型號代碼,如您要求 – BeachRunnerFred 2011-04-22 20:44:18
謝謝。如果要存儲日期,則應該將start_time存儲爲'datetime'字段 – fl00r 2011-04-22 21:03:52