내 앱은 has_many : through 연결을 사용하며 연결 및 연결 자체에서 가장 효율적으로 데이터를로드하고 표시하는 방법을 알아 내는데 어려움을 겪고 있습니다.조인 모델 레일에서 애트리뷰트로 열심히로드하기
class Person < ActiveRecord::Base
has_many :people_ranks
has_many :ranks, :through => :people_ranks
has_many :institutions_people
has_many :institutions, :through => :institutions_people
belongs_to :school
belongs_to :department
end
class Institution < ActiveRecord::Base
has_many :institutions_people
has_many :people, :through => :institutions_people
end
class InstitutionsPerson < ActiveRecord::Base
belongs_to :institution
belongs_to :person
end
및 해당 모델 : 내가 @ person.year_hired, @ person.institution.name 같은 것을 가진 사람의 기관 정보를 표시 할
create_table :people, :force => true do |t|
t.string :name
t.string :degree
t.integer :year_grad
t.integer :year_hired
end
create_table :institutions, :force => true do |t|
t.string :name
t.integer :ischool
end
create_table :institutions_people, :id => false do |t|
t.integer :institution_id
t.integer :person_id
t.string :rel_type
end
여기
내 클래스입니다 , @ person.institution.institutions_people.rel_type (여기서 rel_type은 "graduated"또는 "hired :)이지만 세 번째 부분은 작동하지 않습니다. person_controller의 show bit에서 다음을 사용하십시오 :@person = Person.find(params[:id], :include => [:school, :department, :institutions_people, :people_ranks, {:institutions_people => :institution}, {:people_ranks => :rank}])
은 @ person.institutions 및 @ person.institutions_people에 대한 액세스 권한을 부여하지만 join에서 rel_type 속성을 person-institution 관계에 어떻게 연결합니까? (저는 PHP에서부터 SQL을 빌드하고 루프를 작성하는 방법을 설명합니다. 그러나 RoR은 저를 곤혹스럽게합니다.)
"열심히로드하는 중"및 "has_many : through ", 그러나 나는 협회를 만드는 것에 대한 해답을 얻는다. 제 질문은 실제로 협회의 데이터에 액세스하는 것입니다. 내 응용 프로그램은 정적 데이터를 사용하며 업데이트, 파괴 또는 메서드를 걱정하지 않습니다. 도와 줘서 고마워!
감사합니다 @ 릭 나는 많은 기관을 한 번에 얻으려고 @rel_inst = @ me.institutions_people을 사용하는 것을 끝내었다. 매력처럼 작동합니다. 도와 줘서 고마워! – Libby