2
레일스의 belongs_to
개념에 대한 이해가 부족합니다. 문서 상태 :Ruby on Rails의 다중 belongs_to 연관 저장
class Employee < ActiveRecord::Base
belongs_to :department
belongs_to :city
belongs_to :pay_grade
end
윌 다음 코드 화재 세 가지 업데이트 그렇다면 그것을 할 수있는 더 좋은 방법이있다 :
Adding an object to a collection (has_many or has_and_belongs_to_many) automatically saves that object
이의 난이 Employee
엔티티 있다고 가정 해 봅시다? :
e = Employee.create("John Smith")
Department.find(1) << e
City.find(42) << e
Pay_Grade.find("P-78") << e
하지만 * departement.employees << e * 양식을 사용하면 어떻게됩니까? RoR은 저장하기 전에 마지막 * collection # << * 작업까지 기다리겠습니까? 아니면 매번 하나의 update 명령문을 실행합니까? – d0bz
'<<'는 데이터베이스에 즉시 씁니다. – Femaref