레일즈 앱에서 has_many 관계가있는 클래스가 있습니다. 효율성을 위해 데이터베이스의 많은 행을 한 번에 업데이트하기 위해 일부 직접 SQL을 수행하고 has_many 관계를 더 이상 유효하지 않게 표시하려고합니다. 나중에 코드가 has_many 관계에 액세스하면 데이터를 다시로드하려고합니다. 하지만 필연적으로 SQL을 건너 뛰고 싶습니다. 그래서 예를 들면레일에서 has_many 관계를 무효화하는 방법
:
class Student
has_many courses # may have a :condition clause or some such
def some_method
# For some reason, we want to change all courses in here, not
# just a single row.
ActiveRecord::Base.connection.execute("UPDATE courses SET location = #{new_location}")
# Not sure if we'll later do anything with self.courses, so I need to invalidate
# that relationship. Could do self.courses.reload right here, but I don't want to
# do the SQL if it isn't necessary; the cache only lasts until the end of the
# current page request.
end
end
내가 아니라 오히려 뭔가를 분명 누락 될 수 있습니다. 일부 가설적인 self.courses.invalidate 메소드.
이것은 내가 필요한 것입니다. 고맙습니다! – ChrisInEdmonton
has_many 문서의 공용 API : collection (force_reload = false) –