鑑於此代碼:的ActiveRecord :: RecordInvalid(驗證失敗:Word不能爲空)
class Word < ActiveRecord::Base
has_and_belongs_to_many :definitions
end
class Definition < ActiveRecord::Base
has_and_belongs_to_many :word
validates :word, presence: true
end
mysql> show columns from definitions_words;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| definition_id | int(11) | NO | PRI | NULL | |
| word_id | int(11) | NO | PRI | NULL | |
+---------------+---------+------+-----+---------+-------+
當我打電話:
word = Word.first
word.definitions.create!
我得到:
ActiveRecord::RecordInvalid (Validation failed: Word can't be blank)
爲什麼你期望它的工作? –
在我看來是好的,應該工作。 –