【发布时间】:2014-01-03 10:14:40
【问题描述】:
鉴于此代码:
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)
【问题讨论】:
-
你为什么期望它可以工作?
-
我认为没问题,应该可以工作。
标签: ruby-on-rails ruby ruby-on-rails-3