belongs_to
在 Rails 的概念中有一些我不太了解的东西。文档指出:
Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object
假设我有一个Employee
实体:
class Employee < ActiveRecord::Base
belongs_to :department
belongs_to :city
belongs_to :pay_grade
end
以下代码会触发三个更新吗?如果是,有更好的方法吗?:
e = Employee.create("John Smith")
Department.find(1) << e
City.find(42) << e
Pay_Grade.find("P-78") << e