튜토리얼을 따라 레서피 웹 사이트에 태그 지정 방법을 만들었습니다. 나는 사용자가 선택한 ALL 태그가 조리법 선택하는 필터를 만들 싶습니다 내 레시피 지수에서레일 4 : 태그가있는 필터
class Recipe < ActiveRecord::Base
has_many :taggings
has_many :tags, through: :taggings
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :recipes, through: :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :recipe
end
: 나는 다음과 같은 모델을 가지고있다.
나는 주어진 태그로 모든 요리법을 당겨하는 방법이 있습니다
def self.tagged_with(name)
Tag.find_by_name!(name).recipes
end
을 ... 그리고 나는 하나의 쿼리에 여러 개의 태그를 결합 inject
를 사용하려고 해요 :
def self.new_filter(tags) # tags is an array
tags.inject(Recipe.all) { |res, arg| res.tagged_with(arg) }
end
하지만 모든 태그가있는 래서 피를 반환하지는 않습니다. 태그 배열에 마지막 태그가있는 래서 피를 반환합니다. 모든 태그가있는 래서 피만 반환하도록 수정하려면 어떻게해야합니까?
이것은 적어도 당신이 가리킬 수있는 대답이있는 http://stackoverflow.com/questions/17920505/match-all-in-active-record-relations-in-a-query와 꽤 비슷한 질문입니다 올바른 방향. – Jim