그 속성에 의해 사용자가 "좋아"한 가지를 검색 (협회를 통해 다형성 has_many) :레일 4 + 태양 흑점 : 나는 레일 4 응용 프로그램에 다음과 같은 뭔가를
class Like < ActiveRecord::Base
belongs_to :liker, class_name: 'User'
belongs_to :likeable, polymorphic: true
end
class Photo < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class Song < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class Video < ActiveRecord::Base
# has a `title` and `description` attributes
has_many :likes, as: likeable
has_many :likers, through: :likes
end
class User < ActiveRecord::Base
has_many :likes, source: :liker
has_many :liked_photos, through: :likes, source: :likeable, source_type: 'Photo'
has_many :liked_videos, through: :likes, source: :likeable, source_type: 'Video'
has_many :liked_songs, through: :likes, source: :likeable, source_type: 'Song'
end
내가 좋아하는 것 가지고에 현재 사용자가 사진, 노래 및 비디오를 검색 할 수있는 검색 양식 (title
또는 description
)이 있지만 검색 결과의 범위는 자신이 좋아하는 것만 가능합니다. 내가 has_many 검색 다루는 다른 게시물을 읽었지만, 나는 물건의 다형성 측면에 혼란스러워지고 있어요. 나는 검색이 같은 것 생각했다 :
@search = Sunspot.search(Photo, Song, Video) { fulltext params[:term] }
를하지만 내가 어떻게 current_user.likes으로 범위를에 혼란 얻었다. 또는 Like
모델을 검색해야합니까? 예를 들면 :
@search = Like.search { fulltext params[:term] }
searchable
방법에있어서, 어느 모델 (들)에 포함되어야한다 무엇
이 가장 속하는가?
class Like < ActiveRecord::Base
searchable do
text :likeable_title do
likeable.title
end
text :likeable_description do
likeable.description
end
integer :liker_id
end
end
을 그럼 난 컨트롤러
@search = Like.search(include: :likeable) do
fulltext params[:term]
with :liker_id, current_user.id
end
그때 각 결과에 대한 likeable
를 사용하여 뷰를 렌더링 할 수있는 다음 검색을 할 수있는 :
만약 당신이 마음에 들지 않는다면, 당신의 문제와 관련된 제 질문을 검토해 주시겠습니까? http://stackoverflow.com/questions/42345963/rails-sunspot-with-polymorphic-association – LearningROR
@LearningROR, 지난 2 년간 Rails 또는 Sunspot에서 실제로 작업 한 적이 없으므로 아마도 좋은 리소스가 아닙니다. 이 지점에서. – robertwbradford