私は最近、レール2.3.3から2.3.8への私のレールのアプリを更新して、このような問題に直面しました:すべてのモデル検証がで正常に動作ブラウザを使用してテストすると完全に無視されます。が無効ですか?コンソールのメソッド。これは私のすべてのモデルに起こります。例えば 、私の会社モデルがあります:のRails 2.3.8検証は(常にtrueを返す有効?)コンソールからは動作しません
今コンソールから会社インスタンスの検証をテストするclass Company < Content
...
# Validation
before_validation :ensure_token_existance
validates_presence_of :name,
:address,
:employee_count,
:category_ids,
:region_ids,
:phone,
:email,
:if => Proc.new { |company| company.step?(1) },
:message => "required field"
validates_presence_of :description,
:if => Proc.new { |company| company.step?(2) },
:message => "required field"
after_update :cache_sweeper
...
:私はhttp://guides.rubyonrails.org/v2.3.8/activerecord_validations_callbacks.html#when-does-validation-happenをチェックしました
$ script/console
Loading development environment (Rails 2.3.8)
>> c = Company.new
=> #<Company id: nil, account_id: nil, name: nil, description: nil, employee_count: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, created_at: nil, updated_at: nil, moderation: nil, token: nil, expired_at: nil, address: nil, phone: nil, fax: nil, email: nil, site: nil, delta: true, position: nil, major_company: false, wizard_step: 0, manager_id: nil, org_form: "", robots: nil, language: nil>
>> c.valid?
=> true
>> c.errors
=> #<ActiveRecord::Errors:0x105004848 @errors=#<OrderedHash {}>, @base=#<Company id: nil, account_id: nil, name: nil, description: nil, employee_count: nil, logo_file_name: nil, logo_content_type: nil, logo_file_size: nil, logo_updated_at: nil, created_at: nil, updated_at: nil, moderation: "draft", token: "mYoXPgNYwxBmdCTI", expired_at: nil, address: nil, phone: nil, fax: nil, email: nil, site: nil, delta: true, position: nil, major_company: false, wizard_step: 0, manager_id: nil, org_form: "", robots: nil, language: nil>>
>> c.save
=> true
>> c.save!
=> true
:
次のメソッドをトリガーオブジェクトが有効である場合にのみ データベースにオブジェクトを保存します:
create create! save save! update update_attributes update_attributes!
save!)レコードが の場合、例外が発生します。 non-bangバージョンはそうではありません:saveおよびupdate_attributes falseを返します。作成して更新するだけでオブジェクト/ sを返します。
ここで何が間違っているかを知る手助けができますか? =)
あなたの '' 'でしょうか:if => Proc.new {| company | company.step?(1)} '' 'は問題を引き起こしていますか?私はあなたがcompany.stepをコンソールに設定しているのを見ていないので。それは、それがそのステップにないので、検証が見過ごされる原因になっている可能性があります。 – Koby
Thx @コビー、本当に問題を引き起こした、あなたの助けをありがとう – Wastrox