has_many :
을 통해 당신은 사용해야 할 겁니다 has_many :through
가입 모델
이것은 many-to-many
관계로 알려진 것입니다 (당신이 repins
을 통해 many users
에 many pins
를 연결할 수 있습니다 의미).
#app/model/pin.rb
Class Pin < ActiveRecord::Base
has_many :repins
has_many :users, through :repins
end
#app/models/repin.rb
Class Repin < ActiveRecord::Base
#fields - id | user_id | pin_id | description | created_at | updated_at
belongs_to :user
belongs_to :pin
end
#app/models/user.rb
Class User < ActiveRecord::Base
has_many :repins
has_many :pins, through: :repins
end
이 당신을 호출 할 수 있습니다 :
@pin = Pin.find params[:id]
@pin.repins.each do |repin|
repin.description
end
당신이 각각의 출처에 필요한 설명을 만들 수있는 기회를 제공 - HMT 설치는 당신에게 가입 기록에 자신의 데이터를 추가 할 수있는 기능을 제공합니다
또는
@user = User.find params[:id]
@user.repins.each do |repin|
repin.description
end
어떻게 설명을 Repin에 입력합니까? 레일즈 마이그레이션 AddDescriptionToRepins'이 될 것인가? –
예, 다음과 같이 열을 추가합니다 :'add_column : repins, : description, : string' –