Rails 3.2 사용. 2 가지 옵션을 원한다고 가정 해 봅시다 :Ruby on Rails의 재귀 루프 개선
- 모든 여행 사진을 가져옵니다.
- 첫 번째 사진을 가져옵니다. 예상대로
# trip.rb class Trip < ActiveRecord::Base has_many :trip_days def trip_photos if (photos = trip_days.map(&:spots).flatten.map(&:photos).flatten.map) photos.each do |photo| photo.url(:picture_preview) end end end def trip_photo trip_photos.first end end # trip_day.rb class TripDay < ActiveRecord::Base belongs_to :trip has_many :trip_day_spots has_many :spots, :through => :trip_day_spots end # trip_day_spot.rb class TripDaySpot < ActiveRecord::Base belongs_to :trip_day belongs_to :spot end #spot.rb class Spot < ActiveRecord::Base end # trips_controller.rb class TripsController < ApplicationController def index @trips = Trip.public.paginate(:page => params[:page], :per_page => 25) end end
의
trip_photos
방법은 SQL 쿼리를 많이 생성 :
나는 다음과 같은 코드가 있습니다. 더 좋은 방법이 있는지 궁금합니다.
감사합니다. 'trip_photo '를 통해'trip_photo'를 실행할 때'#'과 같은 것을 얻었지만, 나가기 때문에 고칠 시간이 없습니다. 그것은'.url (: picture_preview'. –
Victor