在ActiveRecord中关闭验证的回滚事务

时间:2013-09-23 20:47:28

标签: ruby-on-rails ruby transactions rails-activerecord

型号:

class Project < ActiveRecord::Base

  attr_accessible :description, :due_date, :expiration_date, :extra_reward, :hours_needed, :location, :mission, :participants_id, :project_picture, :required_skill, :title, :gnikles, :topic, :community_id
  belongs_to :user
  belongs_to :community
  has_many :skills

  validates :title, :presence => true, :length => { :maximum => 100 }
  validates :hours_needed, :presence => true, :inclusion => { :in => 0..20 }, :numericality => true
  validates :description, :presence => true
  validates :required_skill, :presence => true
  validates :due_date, :presence => true
  #validates :expiration_date, :presence => true

end

尝试创建一个测试项目,我明白了:

1.9.3-p194 :025 > Project.create(:title =>"Testings", :description =>"fdsafdsfsa", :topic => 33, :community_id => 3, :expiration_date => "09/09/2013", :due_date =>"09/28/2013", :hours_needed => 1, :required_skill =>1)
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
 => #<Project id: nil, title: "Testings", description: "fdsafdsfsa", required_skill: 1, extra_reward: nil, expiration_date: "2013-09-09 00:00:00", due_date: nil, hours_needed: 1, views: nil, user_id: nil, topic: 33, gnikels: 0, completed: 0, created_at: nil, updated_at: nil, community_id: 3>

为什么due_date不会被传递,但expiration_date会显示没有问题?

1 个答案:

答案 0 :(得分:1)

因为"09/28/2013"不是有效的日期字符串:

'09/28/2013'.to_date # => ArgumentError: invalid date
'28/09/2013'.to_date # => Sat, 28 Sep 2013
'2013-09-28'.to_date # => Sat, 28 Sep 2013