그래서 나는이 모델이 :레일 : Belongs_to 다형성 협회 + 조건
class Model < ActiveRecord::Base
attr_accessible :to_id, :to_type
belongs_to :to, polymorphic: true
end
belongs_to가 특정 유형에있을 때 나는 또 다른 관계를 추가 할 수 있는지 궁금 :
class Model < ActiveRecord::Base
attr_accessible :to_id, :to_type
belongs_to :to, polymorphic: true
belongs_to :to_user, :foreign_key => :to_id, :conditions => ['to_type = ?', 'User'] # doesn't work
# OR MAYBE
belongs_to :to_user, :foreign_key => :to_id, :foreign_class => 'User' # It doesn't check on Model's to_type...
end
그래서 my_model.to_user
을 그 존재하는 경우 user
을 반환하고 설정되지 않았거나 다른 클래스 인 경우 nil
을 반환합니다.
레일 사용하기 3.2
감사합니다!