class User < ActiveRecord::Base
has_many :friends
accepts_nested_attributes_for :friends
end
class Friend < ActiveRecord::Base
belongs_to :user
end
用户将通过 REST API 不断添加朋友:
{ "user": {
"name": "Peter",
"friends_attributes": [
{ "name": "Paul" },
{ "name": "Mary" }
]
}
}
稍后,用户将添加更多朋友并调用相同的 API:
{ "user": {
"name": "Peter",
"friends_attributes": [
{ "name": "Paul" },
{ "name": "Mary" },
{ "name": "John" }
]
}
}
现在,我应该如何编写验证:
- 只添加新朋友(即约翰)
- 不复制现有的(即保罗,玛丽)
- 不要让整个 API 调用失败