레일에서 ActiveRecord의 find
메소드 중 하나를 호출 할 때 :include
해시 값을 전달하는 몇 가지 예를 보았습니다. 그러나 관계 방법을 통해 이것이 가능한지 여부에 대한 예는 보지 못했습니다. 예를 들어, 다음과 같은 I가 있다고 가정 해 봅시다 :레일에서 N + 1 쿼리 방지
User.find(:all, :include => :favorite)
그러나 내가 어떤 예 관계의 사용을 보여주는 표시되지 않습니다 :
def User < ActiveRecord::Base
has_many :user_favorites
has_many :favorites, :through => :user_favorites
end
def Favorite < ActiveRecord::Base
has_many :user_favorites
has_many :users, :through => :user_favorites
end
def UserFavorite < ActiveRecord::Base
belongs_to :user
belongs_to :favorite
end
모든 예제는이 같은 쇼 코드를 참조하십시오. 대신이 일을 저에게 할 수 있을까요?
User.favorites(:include => :user)
요즘 [총알 (https://github.com/ 사용 flyerhzm/bullet). – Barry