私は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 [1mUPDATE "メンバシップ" SET出席者=出席者 - 0.3333333333333333 WHERE( "メンバシップ"。 "course_id" = 4)AND( "メンバシップ」。 "" IN(NULL))はPERSON_ID [0メートル
編集:person_ids
は、
def a_method
self.people.all
end
しかし、あなたの質問に答える:あなたは本当にありたいどのような質問
あなたは人を取得しますか:belongs_to a Day? – EnabrenTane
いいえ、その方法は単なる例に過ぎませんでした。私は正確に私が探しているものを明確にするためにコードを更新しました。 – Tyler