저는 Rails 앱 전체에서 association_collection 메서드 "other_ids"를 아무런 문제없이 사용하고 있습니다. 그러나 연관을 정의하는 모델 내에서 액세스하려고 할 때마다 Rails는 내가 무엇을 차지하고 있는지 전혀 알지 못합니다. 예 :Rails의 모델에서 singular_association_ids에 액세스
class Membership < ActiveRecord::Base
belongs_to :course, :touch => true
belongs_to :person, :touch => true
end
class Day < ActiveRecord::Base
belongs_to :course, :touch => true, :counter_cache => true
has_many :presents, :dependent => :delete_all
has_many :people, :through => :presents
before_destroy :clear_attendance
def clear_attendance
mems = Membership.where(:course_id => course.id, :person_id => person_ids)
mems.update_all(["attendance = attendance - ?", (1/course.days.size.to_f)])
end
end
이 경우 person_ids는 항상 null입니다. 나는 self.person_ids, people.ids 등을 시도했습니다. 나는 문제가없는 다른 곳에서 day.person_ids를 사용 했으므로 여기서 왜 그것을 사용할 수 없습니까?
저는 Ruby 1.9.1과 Rails 3.0.3을 사용하고 있습니다. 다음은 내 로그에서 SQL 호출입니다.
[1m [36mAREL (0.0ms) [0m [1mUPDATE "멤버쉽"출석율 = 출석률 - 0.3333333333333333 WHERE ("멤버십". "course_id"= 4) AND ("멤버쉽 "." "person_id로 IN (NULL)) [0m
편집 : 질문을 명확히하기 위해 더 많은 코드를 추가
사람들을 얻고 싶습니까? belongs_to a Day? – EnabrenTane
아니요, 그 방법은 단지 예일뿐입니다. 내가 무엇을 찾고 있는지 명확히하기 위해 코드를 업데이트했습니다. – Tyler